Dropbox API v2 PHP上传文件问题

9

我正在使用Dropbox API v2上传文件。不幸的是,没有针对Dropbox API v2的PHP库。

https://www.dropbox.com/developers/documentation/http/documentation#files-upload

这是我的代码:

$token = 'sometoken'; // oauth token

$headers = array("Authorization: Bearer ". $token,
    'Dropbox-API-Arg: {"path": "/test.txt","mode": "add","autorename": true,"mute":false}',
    "Content-Type: application/octet-stream");


$url = 'https://content.dropboxapi.com/2/files/upload';

$ch = curl_init($url);

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

$path = './google/file.json';
$fp = fopen($path, 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


//curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);


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

curl_close($ch);
echo $response;

它创建了test.txt文件,但是文件大小为0字节。 当我检查代码时,发现它读取的也是0字节。

以下是代码执行的输出结果:

*   Trying 45.58.74.164...
* Connected to content.dropboxapi.com (45.58.74.164) port 443 (#0)
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: C:\xampp\php\extras\ssl\cacert.pem
  CApath: none
* NPN, negotiated HTTP1.1
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
*  subject: OU=Domain Control Validated; CN=dl.dropboxusercontent.com
*  start date: Jul  9 01:10:38 2016 GMT
*  expire date: May  7 20:27:38 2017 GMT
*  subjectAltName: host "content.dropboxapi.com" matched cert's "content.dropboxapi.com"
*  issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
*  SSL certificate verify ok.
> POST /2/files/upload HTTP/1.1
Host: content.dropboxapi.com
Accept: */*
Authorization: Bearer sometoken
Dropbox-API-Arg: {"path": "/test.txt","mode": "add","autorename": true,"mute":false}
Content-Type: application/octet-stream
Expect: 100-continue

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Server: nginx
< Date: Thu, 15 Sep 2016 13:37:16 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< pragma: no-cache
< cache-control: no-cache
< X-Server-Response-Time: 461
< X-Dropbox-Request-Id: 7b0003052a45a92fac0e7afca80f4f4c
< X-Robots-Tag: noindex, nofollow, noimageindex
<
* Connection #0 to host content.dropboxapi.com left intact
{"name": "test.txt", "path_lower": "/test.txt", "path_display": "/test.txt", "id": "id:L2W5zzeox5IAAAAAAAAjvg", "client_modified": "2016-09-15T13:37:16Z", "server_modified": "2016-09-15T13:37:16Z", "rev": "ee6e21bf2e5c", "size": 0}<br/>200<br/>{"name": "test.tx
t", "path_lower": "/test.txt", "path_display": "/test.txt", "id": "id:L2W5zzeox5IAAAAAAAAjvg", "client_modified": "2016-09-15T13:37:16Z", "server_modified": "2016-09-15T13:37:16Z", "rev": "ee6e21bf2e5c", "size": 0}

请阅读http://stackoverflow.com/help/how-to-ask。告诉我们你已经尝试过什么,以及哪些部分没有起作用。 - Alexandre Cartapanis
嗨 @AlexandreCartapanis,我在问题中更新了代码和日志。你能看一下吗?谢谢! - Georgi Kovachev
只是想说市场上有一个适用于PHP的库 https://github.com/Alorel/dropbox-v2-php - Muhammad Omer Aslam
2个回答

9
我找到了一个答案。
$api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url
        $token = 'some value'; // 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);

6

在PHP中,让curl按照Dropbox API期望的方式格式化HTTP请求可能有些棘手。因此,我们可以使用以下方式来替代:

curl_setopt($ch, CURLOPT_POST, true);

请使用以下内容:
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

There's an example here:

$path = 'test_php_upload.txt';
$fp = fopen($path, 'rb');
$size = filesize($path);

$cheaders = array('Authorization: Bearer <ACCESS_TOKEN>',
                  'Content-Type: application/octet-stream',
                  'Dropbox-API-Arg: {"path":"/test/'.$path.'", "mode":"add"}');

$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

echo $response;
curl_close($ch);
fclose($fp);

?>

<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.


谢谢伙计,你救了我的一天。 - dhruv jadia
你好,https://github.com/kunalvarma05/dropbox-php-sdk 与上面的社区sdk相比,这种方法有什么缺点吗? - mrtechmaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接