我真的很灰白的頭髮。2 JSON。只有1個工作。 json_decode php
我想響應爲https://api.gdax.com/products/btc-usd/ticker/
的[問]的數據,但它的返回NULL。
當我嘗試使用幾乎相同的JSON的另一個API,它工作完美。
此示例適用
<?php
$url = "https://api.bitfinex.com/v1/ticker/btcusd";
$json = json_decode(file_get_contents($url), true);
$ask = $json["ask"];
echo $ask;
這個例子返回null
<?php
$url = "https://api.gdax.com/products/btc-usd/ticker/";
$json = json_decode(file_get_contents($url), true);
$ask = $json["ask"];
echo $ask;
任何人那裏有一個很好的解釋,什麼是錯的代碼返回null
將它分開,首先將'file_get_contents()'的結果存儲在一個變量中,然後'echo'它來查看您實際從服務器返回的結果。如果你把這個添加到你的問題中,也許我們可以明白爲什麼'json_decode()'可能會遇到麻煩。 – rickdenhaan
代碼沒有錯。服務器返回狀態代碼400 –