2014-02-27 111 views
1

我試圖從http://api.stackoverflow.com/1.1/search?tagged=php獲取數據。無法從Stack Exchange API獲取數據

我使用這個代碼從API獲取數據:

$url = "http://api.stackoverflow.com/1.1/search?tagged=php"; 
$json = file_get_contents($url); 
$json_data = json_decode($json, true); 
print_r($json); 

但它顯示我什麼。我也用curl來獲取數據,但它也沒有顯示任何內容。爲什麼它沒有向我展示任何東西,我該如何解決這個問題?

+0

當你點擊鏈接您能得到什麼? – snollygolly

回答

3

他們正在返回你壓縮的內容作爲迴應。這就是爲什麼它不適用於你的json解碼。這裏是等同的捲曲請求。

$url= "http://api.stackoverflow.com/1.1/search?tagged=php"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_ENCODING, ""); // this will handle gzip content 
$result = curl_exec($ch); 
curl_close($ch); 
print $result; 
// do json processing here 
+1

打我幾秒鐘:)但我可以證實,這工作。 –

1

的響應gzip壓縮,使用gzdecode

$url = "http://api.stackoverflow.com/1.1/search?tagged=php"; 
$json = file_get_contents($url); 
$json_data = json_decode(gzdecode($json), true); 
print_r($json);