我在php页面中通过file_get_contents获取百度的内容,我的php页面编码是utf-8的,百度的gb2312,我用inconv转码之后输出仍是乱码,是什么原因呢?代码如下:
$url = "http://www.baidu.com/s?f=8&wd=煤层";
$html = file_get_contents($url);
$html = iconv("gb2312", "utf-8//IGNORE",$html);
print_r ($html);
$url = "http://www.baidu.com/s?f=8&wd=煤层";
$html = file_get_contents($url);
$html = iconv("gb2312", "utf-8//IGNORE",$html);
print_r ($html);
header("Content-Type:text/html;charset=utf-8");
$keyworld="煤层";
$keyworld=iconv("utf-8","gb2312",$keyworld);
$url = "http://www.baidu.com/s?f=8&wd=$keyworld";
$html = file_get_contents($url);
$html = iconv("gb2312", "utf-8//IGNORE",$html);
echo $html;
?>
$html = file_get_contents($url);
$html = iconv("gbk", "utf-8//IGNORE",$html);
print_r ($html);
$url = "http://www.baidu.com/s?f=8&wd=煤层";
$html = file_get_contents($url);
gb2312ToUtf8($html);//可判断是字符串或者数组,自动进行转换;
print_r ($html); function gb2312ToUtf8(&$input)
{
if (!is_array($input))
{
$input = iconv('GB2312', 'UTF-8', $input);
}
else
{
foreach ($input as $k=>$v)
{
self::gb2312ToUtf8(&$input["$k"]);
}
}
}
$keyworld="环境样品 水中氟化物含量";
$keyworld=iconv("utf-8","gb2312",$keyworld);
$url = "http://www.baidu.com/s?f=8&wd=$keyworld";
$html = file_get_contents($url);
$html = iconv("gb2312", "utf-8//IGNORE",$html);
print_r($html);错误:
Warning: file_get_contents(http://www.baidu.com/s?f=8&wd=????ѹƷ ˮ?????) [function.file-get-contents]: failed to open stream: HTTP request failed! in E:\PHPnow\htdocs\test2\index2.php on line 6