在Dropbox版本2 Php Api中下载文件的问题

时间:2017-06-04 07:49:37

标签: php dropbox-api

每个人, 我正在使用此代码从Dropbox Version 2 Php Api下载文件。但是我在文件下载方面还没有成功。让我们看看我正在使用的脚本

function dbx_get_file($token, $in_filepath, $out_filepath)
{
$out_fp = fopen($out_filepath, 'w+');
if ($out_fp === FALSE)
    {
    echo "fopen error; can't open $out_filepath\n";
    return (NULL);
    }

$url = 'https://content.dropboxapi.com/2/files/download';

$header_array = array(
    'Authorization: Bearer ' . $token,
    'Content-Type:',
    'Dropbox-API-Arg: {"path":"' . $in_filepath . '"}'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_FILE, $out_fp);

$metadata = null;
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$metadata)
    {
    $prefix = 'dropbox-api-result:';
    if (strtolower(substr($header, 0, strlen($prefix))) === $prefix)
        {
        $metadata = json_decode(substr($header, strlen($prefix)), true);
        }
    return strlen($header);
    }
);

$output = curl_exec($ch);

if ($output === FALSE)
    {
    echo "curl error: " . curl_error($ch);
    }

curl_close($ch);
fclose($out_fp);

return($metadata);
} // dbx_get_file()

在此处调用此功能。

dbx_get_file("<Access-token>", '/Screenshot_1.png', 'Screenshot_1.png');

我还用Dropbox O-auth 2访问令牌替换了这个“访问令牌”。 请告诉我答案我做错了什么?或者有没有其他方法从Dropbox使用DropBox版本2 PHP Api下载文件。 感谢

1 个答案:

答案 0 :(得分:1)

我是使用请求库https://github.com/rmccue/Requests/完成的 这是我的代码

include('Requests-master/library/Requests.php');
Requests::register_autoloader();
$token="Your Access Token is here";
$response = 
Requests::post("https://content.dropboxapi.com/2/files/download", array(
'Authorization' => "Bearer ".$token,
'Dropbox-Api-Arg' => json_encode(array('path' => '/Screenshot_1.png')),
));
$fileContent = $response->body;
/*Download the file using file_put_contents method*/
file_put_contents("Screenshot_1.png",$fileContent);
$metadata = json_decode($response->headers['Dropbox-Api-Result'], true);
echo "File " . $metadata["name"] . " has the rev " . $metadata["rev"] . ".\n";

文件正在成功下载......:)