php中file_get_contents获取网页乱码解决办法
我采集的一个页面,如下gzip
知道原因了我们就好办了,先百度了一下得出是可以改用curl操作。
curl解决
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1228')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1228>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;
}
采用gzip编码格式
file_get_contents解决:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8534')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8534>file_get_contents("compress.zlib://".$url);
无论页面是否经过gzip压缩,上述代码都可以正常工作!
注意:CURL是需要打开的哦。
curl安装:
xp下面的安装
:修改php.ini文件的设置,找到
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2239')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2239>php_curl.dll//取消下在的注释extension=php_curl.dll
linux下面安装:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9590')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9590># wget http://curl.haxx.se/download/curl-7.17.1.tar.gz
# tar zxvf curl-7.17.1.tar.gz //解压
#cd curl-7.17.1
# ./configure –prefix=/usr/local/curl
# make
# make install
这是安装php之前安装的方法.
您可能感兴趣的文章:
php读取远程文件的三种方法分享
php file_get_contents函数抓取页面信息的代码
解决PHP中file_get_contents抓取网页中文乱码问题
php file_get_contents抓取Gzip网页乱码的解决方法
php 获取远程网页内容简单函数
php判断网页是否启用gzip压缩的方法
php网页标题中文乱码如何解决
php file_get_contents抓取https地址报错的解决办法
php 获取网页内容的四种方法
php中curl、fsocket、file_get_content函数比较