2013-06-11 121 views
1

我正在編寫接受比特幣付款的腳本。我的$ json變量返回null。 var_dump()返回NULL。JSON響應的var_dump返回NULL

事情我已經嘗試:1.我已經採取了$ callbackurl的價值和$ recievingaddress直接粘貼網址到瀏覽器,我已經得到了我用json_last_error JSON響應

  1. 並收到了「沒有錯誤」的回覆

  2. 我逃過magic_quotes的,但沒有任何效果

我在做什麼錯?

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

回答

2

不會鏈的捲曲/ 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()); 
} 
+0

非常感謝。我的問題是,捲曲會正確執行。事實證明,對於https,curl必須使用CA進行配置 – user1801060