在自定义PHP中使用V2将文件上传到dropbox

时间:2017-10-11 12:25:35

标签: php api dropbox-api

我已经编写了文件上传代码,它在一台服务器上工作正常但在本地机器上却没有。以下是代码:

<?php

ini_set("display_errors",1);

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

$token = 'fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 

$headers = array('Authorization: Bearer ' . $token,
    'Content-Type: application/octet-stream',
    'Dropbox-API-Arg: ' .
    json_encode(
            array(
                "path" => '/' . basename('image/1st.jpg'),
                "mode" => "add",
                "autorename" => true,
                "mute" => false
            )
    ),
    'Content-Type: application/octet-stream'
);

$ch = curl_init($api_url);

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

$path = 'images/1st.jpg';

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

curl_close($ch);

echo "<pre>response === "; print_r($response); echo "</pre>";
echo "<pre>http_code === "; print_r($http_code); echo "</pre>";


?>

当我在本地运行此代码时,我得到以下输出:

response === 
http_code === 0

在测试服务器上,它产生以下输出:

{"name": "1st.jpg", "path_lower": "/1st.jpg", "path_display": "/1st.jpg", "id": "id:UDbOKdE2bKXXXXXXECg", "client_modified": "2017-10-10T10:05:11Z", "server_modified": "2017-10-10T10:05:11Z", "rev": "4075316e33a", "size": 143578, "content_hash": "f30041XXXXXXXXXXX35ee3cXXXXXXe649afe8d"}
200

这个问题可能的原因是什么?

2 个答案:

答案 0 :(得分:0)

尝试禁用SSL非常主机: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

或者更正确的方法是:

使用https://curl.haxx.se/ca/cacert.pem

的更新证书列表下载文件

将下载的cacert.pem文件移动到系统中的某个安全位置

更新您的php.ini文件并配置该文件的路径:

; Linux and macOS systems
curl.cainfo = "/path/to/cacert.pem"

; Windows systems
curl.cainfo = "C:\path\to\cacert.pem"

答案 1 :(得分:-1)

以下步骤解决了我的问题:

1)从https://curl.haxx.se/ca/cacert.pem

下载最新的cacert.pem

2)将以下行添加到php.ini

相关问题