2012-02-17 89 views
1

我有这样的公共JSON https://raw.github.com/currencybot/open-exchange-rates/master/latest.json,我需要从中提取数据,现在我想是这样的:从JSON数组检索信息与PHP

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

$json = json_decode($input); 

echo $json[rates]->EUR; 

,但我得到一个空白页,什么建议吗?谢谢 !! Ste

+3

你已经尝试'$ json->汇率─> EUR'? – Gumbo 2012-02-17 09:23:56

回答

6

json_decode要么返回一个对象,要么返回一个数组(当您使用第二个参数时)。你正在尝试使用两者。

尝试:

$json = json_decode($input);  
echo $json->rates->EUR; 

OR

$json = json_decode($input, true); 
echo $json['rates']['EUR']; 

至于空白页,请添加以下到脚本的顶部:

error_reporting(E_ALL); 
init_set('display_errors', true); 

500错误表示您无法使用file_get_contents解析该网址。

检查here了解更多信息。

+0

+1不错..比思维/打字快2分钟! – ManseUK 2012-02-17 09:29:25

+0

嗨,大家好,谢谢你的回答,但是仍然一样,空白页:/ – user1215816 2012-02-17 09:30:44

+0

请看我的更新,可以解决你的白屏死机。 – 2012-02-17 09:34:34

0

该链接看起来像以https开头,而file_get_contents无法处理SSL。我的建议是使用curl

+1

file_get_contents如果PHP是使用ssl构建的,或者您已经编写并注册了您自己的https流,则CAN可以处理这些链接 – 2012-02-17 09:28:10

+0

如何使用Curl尝试?谢谢 – user1215816 2012-02-17 09:34:39

0

阅读json_decode帮助...没有它返回的第二个参数和对象不是一个数组...

或者:

$json = json_decode($input); 
echo $json->rates->EUR; 

$json = json_decode($input,true); 
echo $json['rates']['EUR']; 
0

尝试:

$arr = json_decode($input, true); 

var_dump($arr); 
echo $arr['rates']['EUR']; 

延伸阅读: http://de.php.net/manual/de/function.json-encode.php

对于anexample与卷曲和SSL阅读: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

+0

它给“NULL”,不知道为什么:) – user1215816 2012-02-17 09:31:55

+0

然后你从file_get_contents()没有结果。 尝试cURL而不是 – 2012-02-17 09:37:33

+0

你能给我一个例子吗?我正在寻找信息,但我有点困惑,谢谢:) – user1215816 2012-02-17 09:44:11