2013-05-12 122 views
0

我有一個免費的API擺弄,一切都很好,但偶爾我得到下面的錯誤。致命錯誤「無效數據接收」

Fatal error: Uncaught exception 'Exception' with message 'Invalid data received, please make sure connection is working and requested API exists' in /MY SERVER PATH/functions.php:39 Stack trace: #0 /MY SERVER PATH/index.php(5): myFunc('getInfo') #1 {main} thrown in /MY SERVER PATH/functions.php on line 39 

我只是好奇,爲什麼發生這種情況?

下面是引用相關規範。

感謝所有額外的幫助。


SNIP PET FROM的functions.php

function myFunc($method, array $req = array()) { 
    // API settings 
    $key = 'MY API KEY'; // your API-key 
    $secret = 'MY SECRET'; // your Secret-key 

    $req['method'] = $method; 
    $mt = explode(' ', microtime()); 
    $req['nonce'] = $mt[1]; 

    // generate the POST data string 
    $post_data = http_build_query($req, '', '&'); 

    $sign = hash_hmac("sha512", $post_data, $secret); 

    // generate the extra headers 
    $headers = array(
      'Sign: '.$sign, 
      'Key: '.$key, 
    ); 

    // our curl handle (initialize if required) 
    static $ch = null; 
    if (is_null($ch)) { 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); 
    } 
    curl_setopt($ch, CURLOPT_URL, 'URL TO THE API'); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

    // run the query 
    $res = curl_exec($ch); 
    if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch)); 
    $dec = json_decode($res, true); 
    if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists'); 
    if (!$dec){ 
     echo ''; 
    }else{ 
    return $dec; 
    } 

}

段從index.php文件

$result = myFunc("getInfo"); 
+0

在異常消息中包含$ res中的json,所以我們/您可以看到問題所在。 – 2013-05-12 19:24:45

+0

它在底部說得很對,不是嗎?它拋出一個異常,因爲'curl_exec'返回'false'。它爲什麼會返回錯誤?另一端的服務器遇到請求問題。如果它間歇發生,它可能是服務器負載或片狀連接。如果它是可重複的,請求有問題。 – alexis 2013-05-12 19:25:53

+0

@alexis我相信你對服務器負載或片狀連接是正確的,因爲它現在再次正常工作。順便說一句,有沒有一種方法,我們可以探討,如果它真的是錯誤的原因? – 2013-05-12 19:35:16

回答

0

的問題是在這裏

$dec = json_decode($res, true); 
if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists'); 

!$dec是不是就在這裏,因爲服務器可以用falsenull0等等,這很可能是有效的響應,但是你的代碼會把這些與json_decoding一起發生錯誤。 要使用json_decode檢查錯誤,請改爲使用json_last_error。

$dec = json_decode($res, true); 
$err = json_last_error(); 
if ($err !== JSON_ERROR_NONE) throw new Exception('Invalid data received, please make sure connection is working and requested API exists'); 
+0

感謝您指出問題所在。順便說一句,這會解決問題的好處嗎? (抱歉noob問題),因爲現在,代碼工作正常。我只是不知道何時再次發生致命錯誤。它只是不穩定。 – 2013-05-12 20:27:07

+0

oops'致命錯誤:調用未定義的函數json_last_error()'怎麼回事? – 2013-05-12 20:32:44

+0

json_last_error是PHP> = 5.3.x,因此您可能正在使用舊版本。忘記上述內容並在原始代碼中使用'$ dec!== null'而不是'!$ dec'作爲解決方法,直到升級到5.3。我不知道這是否能夠解決您遇到的所有問題,因爲有些例外情況可能是json_decoding的真正問題,我不能說。 – rpkamp 2013-05-12 21:21:09