采集时候页面乱码,分两种情况:第一种是防盗链,第二种是页面需要解压
可以采用 mb_convert_encoding() 函数进行转码
1.防盗链模拟浏览器就可以了(以搜狐首页为例,本人用的是火狐浏览器:Mozilla firefox)
header('content-type:text/html;charset=utf-8');
$url="http://www.sohu.com/";
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');
$html=file_get_contents($url);
echo mb_convert_encoding($html,'utf8','gbk');
2.解压方式(以天气预报为例)
<?php
header("content-type:text/html;charset=utf-8");
$url="http://wthrcdn.etouch.cn/WeatherApi?city=北京";
$xml = simplexml_load_file("compress.zlib://".$url);
$json=json_encode($xml);
$arr=json_decode($json,true);
print_r($arr)