关于同一主题,我的previous question会继续提出这个问题。简单的文件上传就是简单。
$headers = array("Authorization: Bearer dropbox_token",
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/path/file.ext",
"mode":"add"}');
$data = "I love Stackoverflow";
$ch = curl_init('https://content.dropboxapi.com/2/files/upload/');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
但是,当要保存的文件数据达到10兆字节时,这不是一个可行的选择。我以为我会简单地使用新的(ish)PHP curl_file_create。
$headers = array("Authorization: Bearer Dropbox token",
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/path/bigfile.txt",
"mode":"add"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/upload/');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$args['file'] = curl_file_create('/path/to/bigfile.txt');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$args);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
然而,这不起作用。 Dropbox API会返回消息
调用API函数时出错"文件/上传":错误的HTTP"内容类型" header:" application / octet-stream; boundary = --...期待" application / octet-stream"," text / plain;字符集=收存箱-CORS-劈
我非常感谢任何可以告诉我这里可能出错的人。
答案 0 :(得分:2)
看起来curl_file_create
将文件编码为多部分表单上传,与Dropbox API通信时不是您想要的。
如果我理解正确,您尝试解决的问题是您不想将整个文件内容加载到内存中。是吗?
如果是这样,请试一试。 (抱歉,我还没有对它进行过测试,所以它可能包含错误。)
$headers = array('Authorization: Bearer Dropbox token',
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/path/bigfile.txt",
"mode":"add"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$path = '/path/to/bigfile.txt';
$fp = fopen($path, 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
fclose($fp);
echo $response;
请注意,如果您要上传大文件,可能还需要考虑/files/upload_session_start
, etc.。这允许你以块的形式上传,它支持大于150MB的文件(/files/upload
没有)。
答案 1 :(得分:1)
如果有兴趣对CURLOPT_INFILE
无效的Dropbox API v2进行快速修复,请转到此链接Dropbox PHP v2 upload issue
Greg解释并建议使用此代码curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize))
代替curl_setopt($ch, CURLOPT_INFILE, $fp);