2012-02-17 101 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