DropBox API应用程序/八位字节流头 [英] DropBox API application/octet-stream header
问题描述
我正在尝试使用PHP向Dropbox API发出cUrl请求,以便开始上传非常大的zip文件。这是我要实施的文档,位于 https:/ /www.dropbox.com/developers/documentation/http/documentation#files-upload -
I'm attempting to make a cUrl request with PHP to the Dropbox API, in order to begin to upload a very large zip file. Here is the documentation I'm trying to implement, found at https://www.dropbox.com/developers/documentation/http/documentation#files-upload -
URL结构: https://content.dropboxapi.com/2/files/upload_session/start
示例网址请求:
curl -X POST https://content.dropboxapi.com/2/files/upload_session/start \
--header "Authorization: Bearer <get access token>" \
--header "Dropbox-API-Arg: {\"close\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @local_file.txt
这是我的代码:
$uploads = wp_upload_dir();
$file = $uploads['basedir']."/maintainme/backups/files/backup_".$filename.'/'.$filename.'.zip';
$ch = curl_init();
$url = 'https://content.dropboxapi.com/2/files/upload_session/start';
$headers = array(
'Authorization: Bearer ' .$dropbox_token,
'Dropbox-API-Arg: {\"close\": false}',
'Content-Type: application/octet-stream',
);
$fields = array('file' => '@' . $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$result = curl_exec($ch);
curl_close($ch);
我收到的错误消息是:
对API函数文件/上传会话/启动的调用错误:错误的HTTP内容类型标头:应用程序/八位字节流;边界= ----------------- ------- 1ee7d00b0e9b0c47。期望应用程序/八位字节流,文本/纯文本; charset = dropbox-cors-hack之一。
Error in call to API function "files/upload_session/start": Bad HTTP "Content-Type" header: "application/octet-stream; boundary=------------------------1ee7d00b0e9b0c47". Expecting one of "application/octet-stream", "text/plain; charset=dropbox-cors-hack".
似乎这个边界=-每当我尝试发出此请求时,------------等等。谁有想法???谢谢!
It seems that this 'Boundary=------------blahblahblah' gets appended to my content-type header each time I try to make this request. Anyone have any ideas??? Thanks!
推荐答案
解决了!一时兴起,我检查了位于 http://上的 CURLOPT_POSTFIELDS选项php.net/manual/en/function.curl-setopt.php ,下面是它的意思:
Solved it! On a whim, I checked into the 'CURLOPT_POSTFIELDS' option found at http://php.net/manual/en/function.curl-setopt.php, and here's what it said:
完整数据以HTTP POST操作进行发布。要发布文件,请在文件名前添加@并使用完整路径。可以通过在文件名后加上类型; type = mimetype的类型来明确指定文件类型。该参数既可以作为uraracoded字符串(如 para1 = val1& para2 = val2& ...)传递,也可以作为字段名称作为键并将字段数据作为值的数组传递。 如果value是一个数组,则Content-Type标头将设置为multipart / form-data 。从PHP 5.2.0开始,如果使用@前缀将文件传递给此选项,则value必须是一个数组。从PHP 5.5.0开始,不赞成使用@前缀,并且可以使用CURLFile发送文件。通过将CURLOPT_SAFE_UPLOAD选项设置为TRUE,可以禁用@前缀以安全地传递以@开头的值。
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.
相关部分以粗体显示在以上段落中。显然,将数组传递给CURLOPT_POSTFIELDS选项是在内容类型后附加 Boundary = ---- blahblahblah,并导致API调用失败的原因。我更改了
The relevant part is bolded in the paragraph above. Apparently passing an array to the CURLOPT_POSTFIELDS option is what was appending that 'Boundary=----blahblahblah' the Content-Type and causing the API call to fail. I changed
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
到
curl_setopt($ch, CURLOPT_POSTFIELDS, '@'.$file );
并再次尝试。最初的问题已解决,但是我在此部分代码中遇到了 Dropbox-API-Arg行的新问题:
and tried again. The initial issue was fixed, but I then encountered a new issue with the 'Dropbox-API-Arg' line in this section of code:
$headers = array(
'Authorization: Bearer ' .$dropbox_token,
'Dropbox-API-Arg: {\"close\": false}',
'Content-Type: application/octet-stream',
);
结果是,这些参数需要正确进行JSON编码。我是使用以下代码完成此操作的:
Turns out, these arguments need to be properly JSON-Encoded. I did this with the code below:
$args = array('close'=>false);
$args = json_encode($args);
然后更改
'Dropbox-API-Arg: {\"close\": false}'
至:
'Dropbox-API-Arg:'.$args
这是完整的最终代码:
$uploads = wp_upload_dir();
$file = $uploads['basedir']."/maintainme/backups/files/backup_".$filename.'/'.$filename.'.zip';
error_log($file);
$args = array('close'=>false);
$args = json_encode($args);
$ch = curl_init();
$url = 'https://content.dropboxapi.com/2/files/upload_session/start';
$headers = array(
'Authorization: Bearer ' .$dropbox_token,
'Dropbox-API-Arg:'.$args,
'Content-Type: application/octet-stream',
);
$fields = array('file' => '@' . $file);
error_log($fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, '@'.$file );
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$result = curl_exec($ch);
error_log($result);
curl_close($ch);
这篇关于DropBox API应用程序/八位字节流头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!