简体   繁体   English

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

[英]Uploading file to dropbox using Dropbox's v2 API

I want to save an uploaded file in Dropbox using PHP and also use Dropbox's v2 API for this.我想使用 PHP 在 Dropbox 中保存上传的文件,并为此使用 Dropbox 的 v2 API。

I'm not getting any response for this.我没有得到任何回应。

Below is my code.下面是我的代码。

  <?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);

I am getting below response.我得到以下回应。

在此处输入图片说明

After you get access token from Dropbox app run below code your file will uploaded on dropbox从 Dropbox 应用程序获取访问令牌后,运行以下代码,您的文件将上传到 Dropbox

 <?php
$filename = 'pr.jpg';

$api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url
        $token = 'Generated access token'; // 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM