问题是响应也被压缩了。
我首选的解决方法是使用带有 CURLOPT_ENCODING 选项的 curl。
<?php
function curl($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_USERAGENT, 'cURL');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING , "gzip");//<< Solution
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$feed = curl("http://api.stackexchange.com/2.1/users/22656?order=desc&sort=reputation&site=stackoverflow&filter=!*MxOyD8qN0Yghnep");
$feed = json_decode($feed,true);
$rep = $feed['items'][0]['reputation'];
echo $rep;//531776
?>
不过,您可以使用普通的 FGC,然后将响应膨胀回未压缩的状态。
<?php
$feed = file_get_contents('http://api.stackexchange.com/2.1/users/22656?order=desc&sort=reputation&site=stackoverflow&filter=!*MxOyD8qN0Yghnep');
$feed = gzinflate(substr($feed, 10, -8));
$feed = json_decode($feed,true);
$rep = $feed['items'][0]['reputation'];
echo $rep;//531776
?>