采集网址时发现是乱码,但用chrome编码转换后还是乱码,于是确定不是编码问题。
找到原因是,user_agent的问题,需要curl模拟浏览器访问才能得到正常的数据。
function curlx()
{
header("Content-Type:text/html; charset=GBK");
$url = "http://tv.sohu.com/s2013/wstzbzhfh/";
$ch = curl_init();//curl
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$str = curl_exec($ch); //得到页面内容
//$str = mb_convert_encoding($output, "utf-8", "GBK");
echo($str);
curl_close($ch);//释放句柄
}