php
  • api
  • dropbox-api
  • 2017-10-11 69 views 0 likes 
    0

    我已經編寫了文件上傳代碼,它在一臺服務器上工作正常,但在本地計算機上工作正常。以下是代碼:使用V2在自定義PHP中上傳文件到Dropbox

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

    什麼都可以這個問題的可能原因?

    +0

    什麼curl_error($ CH );顯示? –

    +0

    SSL證書問題:無法獲得本地發行者證書 – RokiE

    回答

    0

    以下步驟:

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

    2下載最新的cacert.pem)增加以下線爲php.ini

    0

    嘗試禁用SSL veryfy主持人: 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" 
    
    +0

    我認爲這不是一個答案,所以應該發佈爲評論而不是問題。 – RokiE

    相關問題