【问题标题】:cURL POST file to OneDrivecURL POST 文件到 OneDrive
【发布时间】:2014-10-08 07:59:23
【问题描述】:

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

    $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;名称=“文件”;文件名="[文件名]"'。"

不确定我哪里出错了,但这是预期的 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 url 直接放在表单的操作属性中并将文件输入字段重命名为“文件”时,文件会被上传。但是后来我只是在我的页面上打印了响应 :) 当然不想发生这种情况。

 <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 格式不是很适合,因此请求需要看起来几乎与示例完全相同。见stackoverflow.com/questions/26225335/…
  • 我现在可以上传文件了。但奇怪的是,如果文件是 powerpoint,它总是会在 OneDrive 上被破坏......我将很快将我的代码发布到 GitHub,以便人们可以协作。

标签: php curl onedrive


【解决方案1】:

可以像这样使用带有 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); 

【讨论】:

    【解决方案2】:

    直接来自https://developer.microsoft.com/en-us/onedrive

    curl https://graph.microsoft.com/v1.0/me/drive/root:/document1.docx:/content -X PUT -d @document1.docx -H "Authorization: bearer access_token_here"
    

    【讨论】:

    【解决方案3】:

    显然,将文件发布到 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 响应。所以如果有人知道怎么做,请告诉我!

    【讨论】:

    • 这不正确,有可能(见我的回答)
    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 2014-03-15
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多