【问题标题】:Uploading a profile picture via PHP API通过 PHP API 上传个人资料图片
【发布时间】:2013-09-06 20:59:36
【问题描述】:

我在为用户上传个人资料图片时遇到问题。我不断收到 404 错误,API 文档告诉我找不到配置文件。但是,在我稍后将显示的代码上方,我有检索配置文件的代码,并且对于我正在使用的特定 userId,它确实存在。另外:

  • 这是与 PHP SDK 一起使用的
  • 我用来进行身份验证的帐户确实有权编辑用户配置文件
  • 我正在使用的测试图像确实存在并且我能够阅读它

这是我的代码。这有点草率,但一旦我为这个特定的测试用户完成这个,我会清理它:

$file = "testimage.jpeg";
$image_data = file_get_contents($file);

// Build our data
$uid = uniqid();
$data = "--" . $uid . "\r\n".
    "Content-Disposition: form-data; name=\"profileImage\"; filename=\"profileImage.jpeg\"\r\n".
    "Content-Type: image/jpeg\r\n".
    "\r\n".
    $image_data . "\r\n".
    "--" . $uid . "--";

$success = false;
$tryAgain = true;
$numAttempts = 1;
$url = "/d2l/api/lp/1.0/profile/user/".$userId."/image";
$uri = $opContext->createAuthenticatedUri($url, "POST");
curl_setopt($ch, CURLOPT_URL, $uri);
while ($tryAgain && $numAttempts < MAX_NUM_ATTEMPTS) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Disposition: multipart/form-data; boundary='.$uid,
        'Content-Length: ' . strlen($data))
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $response = curl_exec($ch);
    $httpCode  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $responseCode = $opContext->handleResult($response, $httpCode, $contentType);
    if ($responseCode == D2LUserContext::RESULT_OKAY) {
        $success = true;
        $tryAgain = false;
    }
    elseif ($responseCode == D2LUserContext::RESULT_INVALID_TIMESTAMP) {
        // Try again since time skew should now be fixed.
        $tryAgain = true;
    }
    else {      // Something bad happened
        echo "here:\r\n".
            $httpCode.
            "\r\n\r\n".
            $responseCode;
        exit;
    }
    $numAttempts++;
}

我不知道自己错过了什么。任何建议将不胜感激。谢谢

编辑:我刚刚注意到 API 文档中的这一部分:

注意

要使用此操作,服务必须已授予应用程序特定权限以执行此操作(即,授予特定应用程序 ID 和密钥以尝试此操作的权限)。

我会询问我们的应用 ID/Key 是否确实有权限。我以为是的,但也许我得到了不正确的信息。这个我去问问。

【问题讨论】:

    标签: php image profile desire2learn valence


    【解决方案1】:

    这是我使用的一个有效的函数。我使用 PHP Api 获取用户上下文对象并通过 curl 完成其余工作。

    $opContext - 来自 PHP API

    $user_id - 来自 d2l

    $filename - 服务器上的图像文件名

    $filepath - 服务器上文件的路径(我在不同的地方有教师和学生)

    $filetype - 用于 mimetype

    static function set_user_image($opContext,$user_id,$filename,$filepath,$filetype){
        $fp = fopen($filepath.$filename, 'r');
        $contents = fread($fp, filesize($filepath.$filename));
        fclose($fp);
        $random_hash = "xxBOUNDARYxx";
        $request ="--".$random_hash."\r\nContent-Type: application/json\r\n\r\n{\"Text\":\"Some comment\", \"HTML\":null}\r\n\r\n--".
                       $random_hash."\r\nContent-Disposition: form-data; name=\"profileimage\"; filename="."\"$filename\""."\r\nContent-Type: image/$filetype\r\n\r\n".
                       $contents."\r\n\r\n--".$random_hash;
    
        $length=strlen($request);
        $url = $opContext->createAuthenticatedUri("/d2l/api/lp/1.1/profile/user/$user_id/image","POST");
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url); 
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/form-data; boundary=xxBOUNDARYxx","Content-Length:".$length));
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST,true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        $response = curl_exec($ch);             
    }   
    

    【讨论】:

    • 非常感谢。这对我有用。我仍在通过它来比较并尝试看看我的代码到底有什么问题(只是为了我自己对未来工作的理解),但是,是的,这就像一个魅力。非常感谢。
    • 太棒了!我很高兴它对你有用。我发现为 D2L API 编写自己的包装库在很多情况下都有帮助。
    • 同意——为了做我自己的内部工作和测试,我做的第一件事就是围绕 D2L Python SDK 编写一组(相当粗略的)包装器,它帮助了一个很好的处理,尤其是处理二进制数据上传等挑剔的操作。
    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 2020-01-10
    • 2014-04-14
    • 2016-05-22
    • 2014-10-05
    • 1970-01-01
    • 2011-06-13
    • 2015-05-22
    相关资源
    最近更新 更多