您的位置:首页 > 其它

获取网页内容时的乱码问题

2013-03-09 09:11 232 查看
现在总结有两个原因:

1.编码问题。

解决:

<?php
$url = "http://news.ef360.com/Articles/2013-3-8/299954.html";
$contents=file_get_contents($url);
$contents=iconv("GBK", "UTF-8//IGNORE", $contents);
echo $contents;
?>


  2.目标页面开了Gzip

解决:@curl获取时

<?php
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;
}
?>


  @file_get_contents获取时

<?php
file_get_contents("compress.zlib://".$url);
?>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: