下面是一些PHP代碼:
$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}
隨機猜測:如果你忘記了後面的換行符e文件內容和結束邊界標記之前,看起來LOR將發送303響應,告訴您轉到404頁面。
它的工作原理非常感謝。我使用的方法不同於手動創建請求內容字符串,所以我沒有完全控制它。謝謝! – Michal