我想從以下網址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上的第一個問題。請讓我知道,如果你想知道更多/如果我應該編輯我的問題。
在此先感謝!
我不認爲你的JSON網址就是你的想法。嘗試使用引號將其包裝起始,因爲它包含空格和分號,這是PHP的特殊字符。 – hrunting 2013-02-10 16:56:32
對不起,這是我的原始代碼引用,這是一個錯字。我認爲json數據是,它是某種包含圖表/圖像信息的數據結構。如果您將URL粘貼到瀏覽器中,它會提示包含數據的下載。我想要做的就是將這些數據以某種方式包含在變量中,這樣我就可以在其上使用'json_decode()'並最終生成一個圖像映射。 – Spiderbro 2013-02-10 21:29:48
如果您將該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