cURL POST文件到OneDrive

问题描述 投票:0回答:2

我正在尝试使用他们的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/". $sekoia_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:表单数据; name =“文件”; 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网址直接放在表单的action属性中并将文件输入字段重命名为'file'时,文件就被上传了。但是然后我只是将响应打印在页面上:)不想当然发生。

 <form action="<?php echo "https://apis.live.net/v5.0/". $sekoia_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>
php curl onedrive
2个回答
0
投票

显然,将文件发布到OneDrive API的唯一方法是像这样直接在您的表单中进行处理。并提供一个redirect_uri。响应将以英镑值的形式出现在您的URL上。这不理想,因为我必须使用javascript获取文件ID。

<form action="<?php echo "https://apis.live.net/v5.0/". $sekoia_folder ."/files?access_token=" . $access_token . "&redirect_uri=http%3A%2F%2Fwww.sekoia-ppt.dev.intracto.com"; ?>" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" value="Upload your ppt" name="btnUpload"/>
</form>

我仍然希望以curl类型的方式执行此操作,从而为我提供了正确的json响应。所以,如果有人知道怎么做,请告诉我!


0
投票

可以这样将CUT与Curl一起使用。在我的示例中,文件直接上传到根目录,您可以指定其他路径。还要更改Mime Type(在我的情况下,这是一个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); 
© www.soinside.com 2019 - 2024. All rights reserved.