<?php
echo file_get_contents("http://www.google.com.hk/search?q=".iconv('GBK','UTF-8','速购吧'));
?>用了CURL这个也好像不行啊。别的网页好像都没问题,google网页老不一样,就是要怎样才能得到和浏览的一样呢?
谢谢!

解决方案 »

  1.   

    curl訪問要使用cookie,否則google是不返回正常的網頁的。
      

  2.   

    $cookie_file = dirname(__FILE__)."/cookie.txt";$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;
      

  3.   

    编码问题,www.google.com.hk搜索结果页面是big5编码
    <?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;
    ?>
      

  4.   


    你得到的google页面内容有一个导航条
    所有網頁 圖片 影片 地圖 新聞 財經 Gmail 更多 ▼
    翻譯 網誌搜尋 更新
    YouTube 日曆 相片 文件 閱讀器 協作平台
    更多 »这些文字是不是乱码?
    如果不是,只能是iconv('GBK','UTF-8','速购吧')这个地方有问题,你确定php文件是gbk编码?如果是,请iconv成big5试下。
      

  5.   

    gbk iconv成big5好像有点问题1.确保你的php文件编码是gbk。用上面的代码
    2.直接把php文件改成utf-8编码,就不用iconv了
      

  6.   


    问题基本解决,再请教一个问题。
    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”却可以。
      

  7.   

    你用CURL得到的是HTML代码,而你在浏览器里看到的是浏览器渲染过后的可视化结果,两个结果永远不会一样的。
      

  8.   

    你好 你的这篇帖子里的问题是怎么解决的?我也遇到了,curl和浏览器得到的结果不一样,我采集的是一个实时更新的网页,用浏览器看到的内容就更新的很快,用curl采集到的内容,有时能和浏览器一样,有时采集到的数据会比浏览器慢,不知道是不是服务器端有什么判断?