<?php
echo file_get_contents("http://www.google.com.hk/search?q=".iconv('GBK','UTF-8','速购吧'));
?>用了CURL这个也好像不行啊。别的网页好像都没问题,google网页老不一样,就是要怎样才能得到和浏览的一样呢?
谢谢!
echo file_get_contents("http://www.google.com.hk/search?q=".iconv('GBK','UTF-8','速购吧'));
?>用了CURL这个也好像不行啊。别的网页好像都没问题,google网页老不一样,就是要怎样才能得到和浏览的一样呢?
谢谢!
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "http://www.google.com.hk/search?q=".iconv('GBK','UTF-8','速购吧'));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);$file_contents = curl_exec($ch);
curl_close($ch);echo $file_contents;
<?php
header("Content-type:text/html;charset=big5");//加个头
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "http://www.google.com.hk/search?q=".iconv('GBK','UTF-8','速购吧'));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);$file_contents = curl_exec($ch);
curl_close($ch);echo $file_contents;
?>
你得到的google页面内容有一个导航条
所有網頁 圖片 影片 地圖 新聞 財經 Gmail 更多 ▼
翻譯 網誌搜尋 更新
YouTube 日曆 相片 文件 閱讀器 協作平台
更多 »这些文字是不是乱码?
如果不是,只能是iconv('GBK','UTF-8','速购吧')这个地方有问题,你确定php文件是gbk编码?如果是,请iconv成big5试下。
2.直接把php文件改成utf-8编码,就不用iconv了
问题基本解决,再请教一个问题。
file_get_contents("http://www.google.com.hk/search?q=%E9%80%9F%E8%B4%AD%E5%90%A7&num=100&hl=zh-CN")
Warning: file_get_contents(http://www.google.com.hk/search?q=%E9%80%9F%E8%B4%AD%E5%90%A7&num=100&hl=zh-CN) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting in D:\lzd\php\aa.php on line 8
为什么这个无法获取到内容,用curl就行。file_get_contents为什么不行?
file_get_contents("http://www.google.com.hk/search?q=%E9%80%9F%E8%B4%AD%E5%90%A7&num=100")
去掉那“&hl=zh-CN”却可以。