Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 11:45:39
Original
1034 people have browsed it

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;
}


source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!