使用v2 API将文件上传到BOX

时间:2015-07-23 08:35:08

标签: php curl box-api

我正在尝试使用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

1 个答案:

答案 0 :(得分:0)

不太理想,但是使用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);