使用PHP+;将包上载到存储库;卷曲
我需要从PHP上传一个SCORM包到D2L实例。 我有所有需要的密钥和数据,因为还有其他查询(例如GET请求获取用户配置文件数据)。我还测试了从Chrome的高级REST插件上传,它工作正常,为Repository.LRWSPublishResult提供了新的学习对象id等 当我尝试使用CURL从PHP上传时(我的代码基于GettingStartedSample),我总是得到“objectmovedto/d2l/error/404”响应。 我已经读到,这可能是由URL的“https”前缀引起的,我应该使用CURLOPT_FOLLOWLOCATION选项,但它没有帮助。它跟随重定向并返回空响应 我向你提出一个请求 /d2l/api/lr/1.0/objects/?repositoryId=REPO\u ID 有人有这个工作代码,可以给我一些提示吗?谢谢 以下是一些PHP代码:使用PHP+;将包上载到存储库;卷曲,php,libcurl,desire2learn,Php,Libcurl,Desire2learn,我需要从PHP上传一个SCORM包到D2L实例。 我有所有需要的密钥和数据,因为还有其他查询(例如GET请求获取用户配置文件数据)。我还测试了从Chrome的高级REST插件上传,它工作正常,为Repository.LRWSPublishResult提供了新的学习对象id等 当我尝试使用CURL从PHP上传时(我的代码基于GettingStartedSample),我总是得到“objectmovedto/d2l/error/404”响应。 我已经读到,这可能是由URL的“https”前缀引起的,
$apiRoute = "/d2l/api/lr/1.0/objects/?repositoryId=" . $repoID;
$id = uniqid();
$data = "--" . $id . "\r\n".
"Content-Disposition: form-data; name=\"Resource\"; filename=\"HW_SCORM.zip\"\r\n".
"Content-Type: application/zip\r\n".
"\r\n".
file_get_contents("HW_SCORM.zip") . "\r\n".
"--" . $id . "--";
$uri = $opContext->createAuthenticatedUri($apiRoute, "PUT");
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data; boundary=" . $id,
"Content-Length: " . strlen($data))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
它将发送如下内容:
Content-Type: multipart/form-data; boundary=ba5567548ef846b595f2d8f0ee5a1ca4
Accept: */*
Content-Length: {size}
--ba5567548ef846b595f2d8f0ee5a1ca4
Content-Disposition: form-data; name="Resource"; filename="HW_SCORM.zip"
Content-Type: application/zip
{file_data}
--ba5567548ef846b595f2d8f0ee5a1ca4--
作为回应,你应该看到
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Length: 73
Content-Type: application/json; charset=UTF-8
Expires: -1
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 17 Apr 2013 21:02:42 GMT
{"IdentId":15805,"Version":1,"ExecutionMessage":null,"ExecutionStatus":0}
随机猜测:如果您忘记了文件内容之后和结束边界标记之前的尾随换行符,LOR似乎会发送303响应,告诉您转到404页。它工作正常,非常感谢。我使用的方法与手动创建请求内容字符串不同,因此我无法完全控制它。感谢