2013-02-10 93 views
0

我想從以下網址JSON數據:獲取JSON內容

https://chart.googleapis.com/chart?cht=gv&chl=digraph框架{1的color = red] 2; 3色=紅色]; 4; 5; 6; 7; 8; 9; 10; 11; 12 [顏色=紅色]; 13; 14 [顏色=紅色]; 15; 16 [色=紅色]; 17 [顏色=紅色]; 2 - > 1; 3 - > 1; 4 - > 1; 5 - > 1; 6 - > 2; 8 - > 9; 9 - > 8; 12 - > 2; 12→3; 12 - > 4; 12 - > 5; 7 - > 6; 7 - > 10; 7 - > 11; 8 - > 7; 13 - > 5; 14 - > 13; 15 - > 6; 16 - > 15; 17 - > 12; 10 - > 4; 11 - > 3; 14 - > 16; 16 - > 17; } & CHOF = JSON

我曾嘗試提出了其他問題,這幾種方法:

$jsonurl = "https://chart.googleapis.com/chart?cht=gv&chl=digraph framework { 1[color=red]; 2; 3[color=red]; 4; 5; 6; 7; 8; 9; 10; 11; 12[color=red]; 13; 14[color=red]; 15; 16[color=red]; 17[color=red]; 2 -> 1; 3 -> 1; 4 -> 1; 5 -> 1; 6 -> 2; 8 -> 9; 9 -> 8; 12 -> 2; 12 -> 3; 12 -> 4; 12 -> 5; 7 -> 6; 7 -> 10; 7 -> 11; 8 -> 7; 13 -> 5; 14 -> 13; 15 -> 6; 16 -> 15; 17 -> 12; 10 -> 4; 11 -> 3; 14 -> 16; 16 -> 17; }&chof=json" 

//Method 1 
$json = file_get_contents($jsonurl); 
// returns an error from the google page: 400. That’s an error. Your client has issued a malformed or illegal request. That’s all we know 

//Method 2 
$curlSession = curl_init(); 
curl_setopt($curlSession, CURLOPT_URL, $jsonurl); 
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true); 

$json_decoded = json_decode(curl_exec($curlSession)); 
curl_close($curlSession); 
// returns NULL 
// This makes sense if the JSON isn't not returned correctly. I think json_decode() returns NULL if the input is not correct json data. 

//Method 3 
$json = file_get_contents(urlencode($jsonurl)); 
// Gives another error: [function.file-get-contents]: failed to open stream: File name too long 

如果我只是張貼在我的代碼的原始JSON數據我的算法的其餘部分工作正常。關鍵是,在我的實際代碼中,url是動態的,我需要能夠從url獲取JSON數據。

這似乎是谷歌的安全問題。但是,我不知道如何解決這個問題。

這是我在Stackoverflow上的第一個問題。請讓我知道,如果你想知道更多/如果我應該編輯我的問題。

在此先感謝!

+0

我不認爲你的JSON網址就是你的想法。嘗試使用引號將其包裝起始,因爲它包含空格和分號,這是PHP的特殊字符。 – hrunting 2013-02-10 16:56:32

+0

對不起,這是我的原始代碼引用,這是一個錯字。我認爲json數據是,它是某種包含圖表/圖像信息的數據結構。如果您將URL粘貼到瀏覽器中,它會提示包含數據的下載。我想要做的就是將這些數據以某種方式包含在變量中,這樣我就可以在其上使用'json_decode()'並最終生成一個圖像映射。 – Spiderbro 2013-02-10 21:29:48

+0

如果您將該URL粘貼到瀏覽器中,瀏覽器會將所有空格和特殊字符轉換爲%轉義序列。嘗試將其粘貼到Chrome中,然後將生成的網址複製出來,然後使用該網址(該網址中沒有空格)。看起來括號也需要被轉義,這些瀏覽器通常不會這樣做。看到這個答案:[如何避免與PHP cURL的URL匹配?](http://stackoverflow.com/questions/5137027/how-do-i-avoid-url-globbing-with-php-curl) – hrunting 2013-02-11 01:01:57

回答

0

您的URL需要通過URI轉義才能被cURL正確使用。如果我改變你的腳本是:

# note that in this string, all special characters (eg. spaces) are %-escaped 
$jsonurl = "https://chart.googleapis.com/chart?cht=gv&chl=digraph%20framework%20{%201[color=red];%202;%203[color=red];%204;%205;%206;%207;%208;%209;%2010;%2011;%2012[color=red];%2013;%2014[color=red];%2015;%2016[color=red];%2017[color=red];%202%20-%3E%201;%203%20-%3E%201;%204%20-%3E%201;%205%20-%3E%201;%206%20-%3E%202;%208%20-%3E%209;%209%20-%3E%208;%2012%20-%3E%202;%2012%20-%3E%203;%2012%20-%3E%204;%2012%20-%3E%205;%207%20-%3E%206;%207%20-%3E%2010;%207%20-%3E%2011;%208%20-%3E%207;%2013%20-%3E%205;%2014%20-%3E%2013;%2015%20-%3E%206;%2016%20-%3E%2015;%2017%20-%3E%2012;%2010%20-%3E%204;%2011%20-%3E%203;%2014%20-%3E%2016;%2016%20-%3E%2017;%20}&chof=json"; 

# your settings here are fine 
$curlSession = curl_init(); 
curl_setopt($curlSession, CURLOPT_URL, $jsonurl); 
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true); 

# XXX: not entirely safe here 
# curl_exec() could dump out something other than JSON if it encounters an error 
# I recommend saving the output of curl_exec($c) separately in case of error 
$json = json_decode(curl_exec($c)); 

然後$json包含的數據對象我期望的那樣。我在Mac上用PHP 5.3.15測試了這個。

+0

謝謝,該URL也適用於我的代碼。儘管如此,還沒有想出如何做到這一點。將所有空格轉換爲%20並將所有'>'轉換爲%3E似乎不夠。 – Spiderbro 2013-02-11 16:25:21

+0

請參閱[url - encodeURI()in PHP?](http://stackoverflow.com/questions/4929584/encodeuri-in-php)的答案,該方法將採用給定的URL並對其進行正確編碼。 – hrunting 2013-02-12 03:23:58

+0

是的,我曾嘗試過。同樣的錯誤,唯一可行的是使用谷歌瀏覽器(複製/粘貼)。這並不完美,但現在我已經在某些情況下工作了。謝謝您的幫助! – Spiderbro 2013-02-12 15:50:18