PHP Dropbox API 下载文件到服务器

问题描述 投票:0回答:2

我正在尝试将文件从 Dropbox 下载到我的服务器,没有返回任何内容,但我知道该文件存在

$headers = array('Authorization: Bearer '.$access_token,
                        'Content-Type: application/json',
                        'Dropbox-API-Arg: {"path": "'.$filepath.'"}');

        $file = fopen($filename, "w+");

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/download");
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_FILE, $file);

        $result = curl_exec($ch);
        
        curl_close ($ch);

        fclose($file);

我哪里做错了?怎么解决?

php dropbox-api
2个回答
0
投票

尝试这种方式并检查结果的输出。并保存到本地服务器。

$url = 'https://content.dropboxapi.com/2/files/download';
        
$formData = [
    "path"=> $filepath
];

$ch = curl_init($url);
$authorization = "Authorization: Bearer ".$access_token;
$request_data = "Dropbox-API-Arg: ".json_encode($formData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, $request_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$result = curl_exec($ch);
curl_close($ch);
dd($result)

0
投票

您需要添加 Content-Type: application/octet-stream 标头而不是 json 标头。

$requestData = [
    'path' => $filepath,
];
$headers = array('Authorization: Bearer '.$access_token,
                    'Content-Type: application/json',
                    'Dropbox-API-Arg: '.json_encode($requestData));

$file = fopen($filename, "w+");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/download");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FILE, $file);

$result = curl_exec($ch);
    
curl_close ($ch);

fclose($file);
© www.soinside.com 2019 - 2024. All rights reserved.