JSON响应的var_dump返回NULL

时间:2013-06-11 15:35:03

标签: php bitcoin

我正在编写一个接受比特币支付的脚本。我的$ json变量返回null。 var_dump()返回NULL。

我尝试过的事情:1。我已经将$ callbackurl和$ recievingaddress的值直接粘贴到我的浏览器中,并获得了JSON响应

  1. 我使用了json_last_error并收到了“无错误”回复

  2. 我已经逃脱了magic_quotes,但这没有效果

  3. 我做错了什么?

        $receiving_address = BITCOIN_ADDRESS;
        if(get_magic_quotes_gpc()){
            $callback_url = urlencode(stripslashes(CALLBACK_URL));
        }  else {
            $callback_url = urlencode(CALLBACK_URL);
        }
    
        $ch = curl_init("https://blockchain.info/api/receive?method=create&address=$receiving_address&shared=false&callback=$callback_url");
        curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $json=json_decode(curl_exec($ch),true);
    
        var_dump($json);
        echo $json[0]->text;
    

    更正后的代码如下:

        $receiving_address = BITCOIN_ADDRESS;
        if (get_magic_quotes_gpc()) {
            $callback_url = urlencode(stripslashes(CALLBACK_URL));
        } else {
            $callback_url = urlencode(CALLBACK_URL);
        }
    
        $ch = curl_init("https://blockchain.info/api/receive?method=create&address=$receiving_address&shared=false&callback=$callback_url");
        curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CAINFO, "C:\Program Files\BitNami WAMPStack\apache2\htdocs\coming\cacert.pem");
    
        $res = curl_exec($ch);
        if ($res === FALSE) {
            die("Curl failed with error: " . curl_error($ch));
        }
    
    
        //var_dump($res);
        $json = json_decode($res, true);
    

1 个答案:

答案 0 :(得分:2)

不要像这样链接你的curl / json调用。你只是假设我们生活在一个完美的世界中,没有什么能够失败。这是一个非常糟糕的决定。始终假设外部资源可以并且将会失败,并检查每个阶段的失败。将您的代码更改为:

$response = curl_exec($ch);
if ($result === FALSE) {
   die("Curl failed with error: " . curl_error($ch));
}
$json = json_decode($response, true);
if (is_null($json)) {
   die("Json decoding failed with error: ". json_last_error());
}
相关问题