DropBox API application/octet-stream header
DropBox API application/octet-stream header
我正在尝试使用 PHP 向 Dropbox API 发出 cUrl 请求,以便开始上传一个非常大的 zip 文件。这是我正在尝试实现的文档,位于 https://www.dropbox.com/developers/documentation/http/documentation#files-upload -
URL结构:https://content.dropboxapi.com/2/files/upload_session/start
示例 cUrl 请求:
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 函数时出错 "files/upload_session/start":HTTP 错误 "Content-Type" header:"application/octet-stream; boundary=------------------------1ee7d00b0e9b0c47"。期待 "application/octet-stream"、"text/plain; charset=dropbox-cors-hack".
之一
似乎每次我尝试发出此请求时,此 'Boundary=------------blahblahblah' 都会附加到我的 content-type header。有人有主意吗???谢谢!
解决了!一时兴起,我检查了 http://php.net/manual/en/function.curl-setopt.php 中的 'CURLOPT_POSTFIELDS' 选项,它是这样说的:
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' Content-Type 并导致 API 调用失败的原因。我改变了
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
至
curl_setopt($ch, CURLOPT_POSTFIELDS, '@'.$file );
并再次尝试。最初的问题已解决,但随后我在这部分代码中的 'Dropbox-API-Arg' 行遇到了一个新问题:
$headers = array(
'Authorization: Bearer ' .$dropbox_token,
'Dropbox-API-Arg: {\"close\": false}',
'Content-Type: application/octet-stream',
);
事实证明,这些参数需要被正确地 JSON 编码。我用下面的代码做到了这一点:
$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);
我正在尝试使用 PHP 向 Dropbox API 发出 cUrl 请求,以便开始上传一个非常大的 zip 文件。这是我正在尝试实现的文档,位于 https://www.dropbox.com/developers/documentation/http/documentation#files-upload -
URL结构:https://content.dropboxapi.com/2/files/upload_session/start
示例 cUrl 请求:
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 函数时出错 "files/upload_session/start":HTTP 错误 "Content-Type" header:"application/octet-stream; boundary=------------------------1ee7d00b0e9b0c47"。期待 "application/octet-stream"、"text/plain; charset=dropbox-cors-hack".
之一似乎每次我尝试发出此请求时,此 'Boundary=------------blahblahblah' 都会附加到我的 content-type header。有人有主意吗???谢谢!
解决了!一时兴起,我检查了 http://php.net/manual/en/function.curl-setopt.php 中的 'CURLOPT_POSTFIELDS' 选项,它是这样说的:
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' Content-Type 并导致 API 调用失败的原因。我改变了
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
至
curl_setopt($ch, CURLOPT_POSTFIELDS, '@'.$file );
并再次尝试。最初的问题已解决,但随后我在这部分代码中的 'Dropbox-API-Arg' 行遇到了一个新问题:
$headers = array(
'Authorization: Bearer ' .$dropbox_token,
'Dropbox-API-Arg: {\"close\": false}',
'Content-Type: application/octet-stream',
);
事实证明,这些参数需要被正确地 JSON 编码。我用下面的代码做到了这一点:
$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);