2013-05-12 123 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