使用Dropbox的v2 API将文件上传到Dropbox

时间:2018-05-31 13:20:16

标签: php dropbox dropbox-api

我想使用PHP在Dropbox中保存上传的文件,并为此使用Dropbox的v2 API。

我没有得到任何回应。

以下是我的代码。

  <?php
$filename = 'qw.txt';

$api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url
        $token = '<REDACTED>'; // oauth token

        $headers = array('Authorization: Bearer '. $token,
            'Content-Type: application/octet-stream',
            'Dropbox-API-Arg: '.
            json_encode(
                array(
                    "path"=> '/'. basename($filename),
                    "mode" => "add",
                    "autorename" => true,
                    "mute" => false
                )
            )

        );

        $ch = curl_init($api_url);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);

        $path = $filename;


        $fp = fopen($path, 'rb');
        $filesize = filesize($path);

        curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($ch, CURLOPT_VERBOSE, 1); // debug

        $response = curl_exec($ch);


        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        echo($response.'<br/>');
        echo($http_code.'<br/>');

        curl_close($ch);

我的反应低于预期。

enter image description here

1 个答案:

答案 0 :(得分:2)

从Dropbox应用获取访问令牌后,运行以下代码,您的文件将上传到Dropbox中

defaults