cURL上传文件到OneDrive

3

我正在尝试使用OneDrive的API将文件上传到用户文件夹中。

    $cfile = curl_file_create(realpath($_POST['ppt-file']));

    //place file in folder
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://apis.live.net/v5.0/". $my_folder ."/files?access_token=" . $access_token);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $cfile);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $upload_result = trim(curl_exec($ch));
    curl_close($ch);

我从API得到了响应。

请求实体主体在“Content-Disposition”头中具有不正确的值。该值的预期格式为“Content-Disposition:form-data; name="file"; filename="[FileName]"”。

不确定我做错了什么,但这是预期的http标头。

POST https://apis.live.net/v5.0/me/skydrive/files?access_token=ACCESS_TOKEN

Content-Type: multipart/form-data; boundary=A300x

--A300x
Content-Disposition: form-data; name="file"; filename="HelloWorld.txt"
Content-Type: application/octet-stream

Hello, World!
--A300x--

感谢您的提前帮助!
更新: 当我直接将API网址放入表单的操作属性中,并将文件输入字段重命名为“file”时,文件会被上传。但我只是在我的页面上打印了响应,这当然不是我想要发生的事情。
 <form action="<?php echo "https://apis.live.net/v5.0/". $my_folder ."/files?access_token=" . $access_token; ?>" method="post" enctype='multipart/form-data'>
    <input type="file" name="file"/>
    <input type="submit" value="Upload your ppt" name="btnUpload"/>
 </form>

你是否有CURL生成请求的跟踪记录?不幸的是,API的多部分POST格式并不是很适应性强,因此请求将需要几乎与示例完全相同。请参见http://stackoverflow.com/questions/26225335/the-request-entity-body-has-an-incorrect-value-in-the-content-disposition-head - Brad
我现在能够上传文件了。但奇怪的是,如果文件是PowerPoint格式,它总是在OneDrive上损坏... 我很快会将我的代码发布到GitHub上,以便人们可以协作。 - axelvnk
3个回答

4

如何获取访问令牌?我只得到了“访问令牌验证失败。无效的受众。”谢谢! - mud1e
认证是一个相对复杂的主题,请从这里开始阅读:https://learn.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/authentication?view=odsp-graph-online - Thinkeye

3

可以使用Curl的PUT方法来实现,示例如下。在我的例子中,文件被直接上传到了根目录,你也可以指定不同的路径。同时需要更改Mime类型(在我的例子中是jpg文件)。当然,你还需要一个有效的身份验证令牌。

// Get the Binary Content of the File You want to upload
$postdata = file_get_contents($uploadfile);

$upload_path = 'root:'; 
$filename = 'upload.jpg'; 

$ch = curl_init('https://graph.microsoft.com/v1.0/me/'.$upload_path.'/'.$filename.':/content'); 
$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: image/jpeg' , $authorization )); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$result = curl_exec($ch); 
curl_close($ch); 

0
显然,将文件POST到OneDrive API的唯一方法是像这样直接在表单中进行操作,并提供redirect_uri。响应将以附加到您的URL的哈希值的形式返回。这并不理想,因为我将不得不使用JavaScript获取我的文件ID。
<form action="<?php echo "https://apis.live.net/v5.0/". $your_folder ."/files?access_token=" . $access_token . "&redirect_uri=http%3A%2F%2Fwww.whateverredirect.com"; ?>" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" value="Upload your ppt" name="btnUpload"/>
</form>

我仍然希望以curl的方式完成这个操作,以便得到一个正确的JSON响应。如果有人知道如何做,请告诉我!


这不正确,可能性是存在的(请看我的回答)。 - user6057915

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接