使用Dropbox的v2 API将文件上传到Dropbox [英] Uploading file to dropbox using Dropbox's v2 API

查看:95
本文介绍了使用Dropbox的v2 API将文件上传到Dropbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

下面是我的代码。

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

我的反应不佳。

推荐答案

从Dropbox应用获取访问令牌后,运行以下代码将上传到保管箱

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

这篇关于使用Dropbox的v2 API将文件上传到Dropbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆