gzdecode 和 PHP 与 curl 中的file_get_contents


$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)) ;

这在今天玩堆栈溢出 API 时给我带来了问题

替代方法 - 只需使用 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);

相关内容

  • 没有找到相关文章

最新更新