首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用v2接口将文件上传到盒子中

使用v2接口将文件上传到盒子中
EN

Stack Overflow用户
提问于 2015-07-23 16:35:08
回答 1查看 919关注 0票数 3

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

根据文档,curl请求是:

代码语言:javascript
复制
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

我正在使用这个方法:

代码语言:javascript
复制
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);
} 

我得到的回应是:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2015-07-24 20:51:34

不是很理想,但是可以使用shell_exec来工作:

代码语言:javascript
复制
$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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31582173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档