php vs curl中的gzdecode和file_get_contents

时间:2015-12-26 19:30:52

标签: php curl stackexchange-api

$url = 'https://api.stackexchange.com/2.2/answers/'.$id.'?order=desc&sort=activity&site=stackoverflow&filter=!-*f(6t*ZdXeu&key=MY_KEY';
gzdecode(file_get_contents ($url)) ;

今天,当我使用stackoverflow API

时,这给我带来了麻烦

1 个答案:

答案 0 :(得分:0)

替代方法 - 只使用CURL,它会自动解压缩

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 400); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
curl_setopt($curl, CURLOPT_MAXREDIRS, 5); //if http server gives redirection responce
curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // the page encoding

$data = curl_exec($curl); // data already decompressed
curl_close($curl);