使用PHP从JSON数组中检索信息

1 php json

我有这个公共JSON https://raw.github.com/currencybot/open-exchange-rates/master/latest.json,我需要从中提取数据,现在我尝试过这样的事情:

$input = file_get_contents("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json");

$json = json_decode($input);

echo $json[rates]->EUR;
Run Code Online (Sandbox Code Playgroud)

但我得到一个空白页,任何建议?谢谢 !!STE

Wes*_*orp 6

json_decode要么返回一个对象,要么返回一个数组(当你使用第二个参数时).你试图使用两者.

尝试:

$json = json_decode($input);    
echo $json->rates->EUR;
Run Code Online (Sandbox Code Playgroud)

要么

$json = json_decode($input, true);
echo $json['rates']['EUR'];
Run Code Online (Sandbox Code Playgroud)

至于空白页面,请在脚本顶部添加以下内容:

error_reporting(E_ALL);
init_set('display_errors', true);
Run Code Online (Sandbox Code Playgroud)

500错误表示您无法使用file_get_contents解析该URL.

点击此处了解更多信息.