我需要将大于4MB的文件大小上传到onedrive帐户。我正在尝试使用PHP和curl。是否有人尝试过这个选项,请帮我解决这个问题。
答案 0 :(得分:3)
$网址=' https://graph.microsoft.com/v1.0/me/drive/root:/filename:/createUploadSession&#39 ;;
$data= '{}';
$header = array('Content-Type: json',
"Cache-Control: no-cache",
"Pragma: no-cache",
"Authorization: bearer {Access Token}");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
从结果中获取uploadURL,
$graph_url = $result['uploadUrl'];
$fragSize = 320 * 1024;
$file = file_get_contents($filename_location);
$fileSize = strlen($file);
$numFragments = ceil($fileSize / $fragSize);
$bytesRemaining = $fileSize;
$i = 0;
$ch = curl_init($graph_url);
while ($i < $numFragments) {
$chunkSize = $numBytes = $fragSize;
$start = $i * $fragSize;
$end = $i * $fragSize + $chunkSize - 1;
$offset = $i * $fragSize;
if ($bytesRemaining < $chunkSize) {
$chunkSize = $numBytes = $bytesRemaining;
$end = $fileSize - 1;
}
if ($stream = fopen($filename_location, 'r')) {
// get contents using offset
$data = stream_get_contents($stream, $chunkSize, $offset);
fclose($stream);
}
$content_range = " bytes " . $start . "-" . $end . "/" . $fileSize;
$headers = array(
"Content-Length: $numBytes",
"Content-Range:$content_range"
);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, constant('CURL_SSL_VERIFYPEER_STATUS'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$bytesRemaining = $bytesRemaining - $chunkSize;
$i++;
}
当你传递最后一组数据时,它应该是正确的数据字节。否则它将无法上传会话。
答案 1 :(得分:2)
您的申请需要:
有关每个步骤的请求类型以及JSON或http响应代码的详细信息,请参阅OneDrive开发人员中心的上传大文件以及上传会话文档页面:https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession#create-an-upload-session期望的。
从PHP功能的角度来看,您需要:
202 Accepted
),HTTP 200 OK
或HTTP 201 Created
)如果检测到文件名冲突(响应代码为HTTP/1.1 409 Conflict
):
HTTP/1.1 204 No Content
的回复代码。答案 2 :(得分:0)
此代码对我有用:
<?php
$fileName="myfile.zip";
$filename_location=realpath($fileName);
$token="{access token}";
$ch = curl_init();
$url="https://graph.microsoft.com/v1.0/me/drive/root:/api/$fileName:/createUploadSession";
curl_setopt($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$data= '{
"item": {
"@microsoft.graph.conflictBehavior": "rename",
"description": "description",
"fileSystemInfo": { "@odata.type": "microsoft.graph.fileSystemInfo" },
"name": "'.$fileName.'"
}
}';
$header = array(
'Content-Type: application/json',
"Cache-Control: no-cache",
"Pragma: no-cache",
"Authorization: bearer $token");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = json_decode(curl_exec($ch)) ;
$graph_url = $result->uploadUrl;
$fragSize = 320 * 1024;
$file = file_get_contents($filename_location);
$fileSize = strlen($file);
$numFragments = ceil($fileSize / $fragSize);
$bytesRemaining = $fileSize;
$i = 0;
$ch = curl_init($graph_url);
while ($i < $numFragments) {
$chunkSize = $numBytes = $fragSize;
$start = $i * $fragSize;
$end = $i * $fragSize + $chunkSize - 1;
$offset = $i * $fragSize;
if ($bytesRemaining < $chunkSize) {
$chunkSize = $numBytes = $bytesRemaining;
$end = $fileSize - 1;
}
if ($stream = fopen($filename_location, 'r')) {
// get contents using offset
$data = stream_get_contents($stream, $chunkSize, $offset);
fclose($stream);
}
$content_range = " bytes " . $start . "-" . $end . "/" . $fileSize;
$headers = array(
"Content-Length: $numBytes",
"Content-Range:$content_range"
);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$bytesRemaining = $bytesRemaining - $chunkSize;
$i++;
}
?>