cURL POST文件到OneDrive [英] cURL POST file to OneDrive
问题描述
我正在尝试使用他们的API将文件上传到OneDrive上的用户文件夹.
I'm trying to upload a file to a users folder on OneDrive using their 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:表单数据;name =文件";filename ="[FileName]"'."
The request entity body has an incorrect value in the 'Content-Disposition' header. The expected format for this value is 'Content-Disposition: form-data; name="file"; filename="[FileName]"'."
不知道我要去哪里错,但这是预期的http标头.
Not sure where I'm going wrong, but this is the http header that's expected.
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'时,文件就被上传了.但是然后我只是将响应打印在页面上:)不想当然发生.
Update: When I put the API url directly in my action attribute of my form and rename my file input field to 'file', the file gets uploaded. But then I just get the response printed on my page :) not want I want to happen ofcourse.
<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>
推荐答案
显然,将文件发布到OneDrive API的唯一方法是直接以这种形式在文件中进行处理.并提供一个redirect_uri.响应将以英镑值的形式出现在您的URL上.这不理想,因为我必须使用javascript获取文件ID.
Apparently the only way to do POST a file to the OneDrive API is by doing it directly in your form like this. And providing a redirect_uri. The response will come in the form of a pound value attached to your URL. Which is not ideal, since I'll have to get my file id with javascript.
<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响应.所以,如果有人知道怎么做,请告诉我!
I'd still like to do this in a curl type fashion, giving me a proper json response. So if anyone knows how, please do let me know!
这篇关于cURL POST文件到OneDrive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!