我正在尝试使用API从Box下载文件,但我没有得到任何回复。
这是我的代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/files/3934151224/content");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: BoxAuth api_key={myAPIkey}&auth_token={myToken}"));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);
当我尝试运行它时,我得到一个空白页面,没有响应,也没有“另存为”窗口。
我错过了什么。请帮忙。 提前谢谢。
答案 0 :(得分:0)
文件有多大?由于以下两个原因之一(或两者兼而有之),您可能已经内存不足:
答案 1 :(得分:0)
我来到这个解决方案:
//set the headers
$headers = array('Authorization: BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN');
//set the options
curl_setopt($curl, CURLOPT_URL, "https://api.box.com/2.0/files/".$fileid."/content");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //returns to a variable instead of straight to page
curl_setopt($curl, CURLOPT_HEADER, true); //returns headers as part of output
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //I needed this for it to work
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //I needed this for it to work
$headers = curl_exec($curl); //because the returned page is blank, this will include headers only
//parses the headers string into an array
$items = array();
$item = strtok($headers, " ");
while ($item !== false) {
$items[] = $item;
$item = strtok(" ");
}
//redirects to the 14th item in the array (link value) - 17 characters because of
//dodgy header parsing above.
header('Location: '.substr($items[14], 0, -17));
echo curl_error($curl);
curl_close($curl);
这是因为Box API实际上返回一个空白页面,其中包含302重定向标题,而不是文件内容。这样你就可以抓住标题并摆弄它,而不必在发送到客户端之前在服务器上下载文件。
我知道我处理标题字符串的方式有点hacky,但这不是重要的一点,我稍后会清理它。
答案 2 :(得分:0)
我使用以下代码下载文件夹
$url = "https://api.box.com/2.0/files/$fileId/content?access_token=$accessToken"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$contents = curl_exec($ch);
set_time_limit(0);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename='.$filename);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: '.$filesize);
print $contents;