PHP中使用file_get_contents()抓取网页乱码的问题
2013-09-12 20:49
741 查看
如果是编码问题导致的很容易,把抓取到的内容转换一下编码就可以了$content=iconv("GBK","UTF-8",$content),这里我们主要讨论如何抓取开了gzip的页面,如何判断页面开了gzip呢,获取的header头中含有Content-Encoding:gzip;说明页面内容是经过gzip压缩过的,可以通过firebug查看页面是否开启了gzip压缩。
下面介绍一些解决方案:
1、使用PHP自带的zlib库,用下面的代码可以解决:
下面介绍一些解决方案:
1、使用PHP自带的zlib库,用下面的代码可以解决:
$data = file_get_contents("compress.zlib://".$url);2、使用curl代替file_get_contents()函数:
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抓取网页中文乱码问题解决方法
- PHP中使用file_get_contents抓取网页中文乱码问题解决方法
- 解决PHP中file_get_contents抓取网页中文乱码问题
- php用file_get_contents方法抓取网页数据出现乱码的解决方法
- PHP的file_get_contents获取远程页面乱码的问题
- 用file_get_contents抓取网页乱码的2种解决方式
- php 使用file_get_contents的问题
- PHP使用file_get_contents或curl请求https的域名内容为空或Http 505错误的问题排查方法
- 解析PHP中的file_get_contents获取远程页面乱码的问题
- PHP中用file_get_contents抓取网页
- 解析PHP中的file_get_contents获取远程页面乱码的问题【转】
- file_put_contents以及file_get_contents的用法与在使用过程中遇到的问题(PHP学习)
- file_get_contents抓取网页乱码的解决
- 用file_get_contents抓取网页乱码的2种解决方式
- 使用file_get_contents()获取sohu博客乱码问题
- file_get_contents抓取网页乱码的解决
- php发送Http请求,抓取网页数据方法(cURL,file_get_contents,snoopy)
- file_get_contents 抓取网页乱码。
- file_put_contents以及file_get_contents的用法与在使用过程中遇到的问题(PHP学习)
- 解析PHP中的file_get_contents获取远程页面乱码的问题