1

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

当我尝试运行它时,我得到一个空白页,没有任何响应,也没有“另存为”窗口。

我错过了什么。请帮忙。提前致谢。

4

3 回答 3

0

How big is the file? You're probably running out of memory, for one of two reasons (or both):

  1. You are storing the entire response in memory by assigning it to a variable. Consider writing the cURL output to a file, using the CURLOPT_FILE option, and a valid file resource.
  2. You may have output buffering enabled, which would also hold all of the output in memory AGAIN, when you print_r the result. You can easily check this by doing a print ob_get_level(); right before your cURL call.
于 2012-11-12T13:35:36.373 回答
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,但这不是重要的一点,我稍后会清理它。

于 2012-11-29T03:48:15.210 回答
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;
于 2015-05-14T19:15:11.933 回答