Dropbox API上传文档大小为零



我正在使用php和curl将文件上传到Dropbox中,但是每次它都在创建空文件。这是我的代码:

        // reading the remote file to get the file size...
        $file = 'https://upload.wikimedia.org/wikipedia/en/5/58/Penny_test.jpg';
        $ch = curl_init($file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);
        $fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);  // Response is 11201 bytes
        // upload file to dropbox
        $token = 'xxxxxxx'
        $headers = [
            "Authorization: Bearer ". $token,
            'Dropbox-API-Arg: {"path": "/Penny_test.jpg", "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);
        $file = 'https://upload.wikimedia.org/wikipedia/en/5/58/Penny_test.jpg';
        $fp = fopen($file, 'rb');
        curl_setopt($ch, CURLOPT_INFILE, $fp);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        $result = curl_exec($ch);
        print_r($result); die;

这是我得到的回应:

{"name": "54.png", "path_lower": "/penny_test.jpg", "path_display": "/Penny_test.jpg", "id": "id:xxxxxxxxxxxxxxx", "client_modified": "2017-12-04T23:35:37Z", "server_modified": "2017-12-04T23:35:37Z", "rev": "bb3a7696db", "size": 0, "content_hash": "xxxxxxxxxxxxxxxxxxxxxxxxxx"}

为什么Dropbox从此上传&我究竟做错了什么?

谢谢。

这里的问题似乎是请求正文中未提供文件数据的方式。

要修复它,替换:

curl_setopt($ch, CURLOPT_POST, true);

with:

curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST')

最新更新