Uploading file to dropbox using Dropbox's v2 API

℡╲_俬逩灬. 提交于 2020-03-23 06:07:28

问题


I want to save an uploaded file in Dropbox using PHP and also use Dropbox's v2 API for this.

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.


回答1:


After you get access token from Dropbox app run below code your file will uploaded on 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);


来源:https://stackoverflow.com/questions/50625348/uploading-file-to-dropbox-using-dropboxs-v2-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!