【问题标题】:uploading files to BOX using v2 API使用 v2 API 将文件上传到 BOX
【发布时间】:2015-07-23 08:35:08
【问题描述】:

我正在尝试使用 curl 将文件上传到 box,我可以创建和查看文件夹但不能上传文件。

根据文档,curl 请求是:

curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" -X POST \
-F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
-F file=@myfile.jpg

我正在使用这种方法:

public function put_file($filename, $name, $parent_id) {
$url = $this->upload_url . '/files/content';

$attributes = array('name' => $name, 'parent' => array('id' => $parent_id));
$params = array('attributes' => json_encode($attributes), 'file' => "@".realpath($filename));
$headers = array("Authorization: Bearer ".$this->access_token); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch); 
curl_close($ch); 

return json_decode($response, true);
} 

我得到的回复是:

Array
(
[url] => https://upload.box.com/api/2.0/files/content
[content_type] => text/html;charset=UTF-8
[http_code] => 400
[header_size] => 243
[request_size] => 228
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.616644
[namelookup_time] => 0.004139
[connect_time] => 0.151089
[pretransfer_time] => 0.465782
[size_upload] => 331
[size_download] => 0
[speed_download] => 0
[speed_upload] => 204
[download_content_length] => 0
[upload_content_length] => 331
[starttransfer_time] => 1.466139
[redirect_time] => 0
[redirect_url] => 
[primary_ip] => 74.112.184.85
[certinfo] => Array
(
) 
[primary_port] => 443
[local_ip] => 213.230.222.4
[local_port] => 52643
)

我已经查看了关于 SO 的类似问题,但看不到我哪里/哪里出错了。

我使用的库是BoxPHPAPI

【问题讨论】:

  • 你能得到正在发出的实际 HTTP 请求的日志吗?如果您使用的是 OS X 或 Linux,一个简单的方法是运行 nc -l localhost 8080,然后向 http://localhost:8080/api/2.0/files/content 发出 cURL 请求而不是 Box。
  • 不幸的是,在 Windows 上,查看了 curl 发送的标头,但无法确定出了什么问题,尝试通过 shell_exec 进行操作,效果很好。我会选择那个选项。
  • 如果有帮助,这里是整个 HTTP 请求应该是什么样子的示例 - stackoverflow.com/a/27759602/108

标签: php curl box-api


【解决方案1】:

不理想,但使用 shell_exec 代替:

$cmd = "curl https://upload.box.com/api/2.0/files/content \
-H \"Authorization: Bearer $this->access_token\" -X POST \
-F attributes='{\"name\":\"$filename\",\"parent\": {\"id\":\"$parent_id\"}}' \
-F file=@\"$filename\"";

shell_exec($cmd);

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多