php file_get_contents返回的数据是乱码

用file_get_contents返回的数据是乱码

 

上网找到了问题,成功解决、

而上面那种全部乱码的,就是因为数据被gzip压缩编码过的。解决方法有以下两种:
1、找个ungzip的函数来转换下
2、给你的url加个前缀,这样调用

$content = file_get_contents("compress.zlib://".$url);

//无论页面是否经过gzip压缩,上述代码都可以正常工作!

 

3、使用curl

function curl_get($url, $gzip=false){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
if($gzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里
$content = curl_exec($curl);
curl_close($curl);
return $content;
}

 

php file_get_contents返回的数据是乱码

上一篇:ElementUI 升级后打包报错,ERROR in xxx.js from UglifyJs


下一篇:postman 配置https 校验证书