Home  >  Article  >  Backend Development  >  解析PHP中的file_get_contents获取远程页面乱码的问题_php技巧

解析PHP中的file_get_contents获取远程页面乱码的问题_php技巧

WBOY
WBOYOriginal
2016-05-17 08:58:37927browse

PHP的file_get_contents获取远程页面内容,如果是gzip编码过的,返回的字符串就是编码后的乱码
1、解决方法,找个ungzip的函数来转换下
2、给你的url加个前缀,这样调用
$content = file_get_contents("compress.zlib://".$url);
无论页面是否经过gzip压缩,上述代码都可以正常工作!
使用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;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn