时时商务社区

标题: 解析PHP中的file_get_contents获取远程页面乱码的问题 [打印本页]

作者: 网络通达    时间: 2018-2-14 08:16

            PHP的file_get_contents获取远程页面内容,如果是gzip编码过的,返回的字符串就是编码后的乱码
1、解决方法,找个ungzip的函数来转换下
2、给你的url加个前缀,这样调用
$content = file_get_contents("compress.zlib://".$url);
无论页面是否经过gzip压缩,上述代码都可以正常工作!
使用curl模块同样可解决问题
[U]复制代码[/U] 代码如下:
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-CGI进程CPU 100% 与 file_get_contents 函数的关系分析
  • 深入php函数file_get_contents超时处理的方法详解
  • file_get_contents("php://input", "r")实例介绍
  • php读取本地文件常用函数(fopen与file_get_contents)
  • PHP file_get_contents设置超时处理方法
  • php 使用file_get_contents读取大文件的方法
  • PHP使用fopen与file_get_contents读取文件实例分享
            




    欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2