繁体   English   中英

php vs curl中的gzdecode和file_get_contents

[英]gzdecode and file_get_contents in php vs curl

$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时,这给我带来了问题

另一种方法-只需使用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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM