gpt4 book ai didi

php - 使用 v2 API 将文件上传到 BOX

转载 作者:搜寻专家 更新时间:2023-10-31 21:57:56 25 4
gpt4 key购买 nike

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

最佳答案

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

关于php - 使用 v2 API 将文件上传到 BOX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31582173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com