$url = "http://www.google.com.hk/search?q=%E5%9B%BD%E5%AE%B6%E7%94%B5%E7%BD%91&hl=zh-CN&newwindow=1&safe=strict&biw=1419&bih=715&prmdo=1&tbs=frm:1&ei=1attTe7qO4jKvQOszpzbBA&start=10&sa=N";
//$url = 'http://www.baidu.com';
echo '##1';
$page =  file_get_contents($url);
var_dump($page);
echo '##2';file_get_contents(http://www.google.com.hk/search?q=%E5%9B%BD%E5%AE%B6%E7%94%B5%E7%BD%91&hl=zh-CN&newwindow=1&safe=strict&biw=1419&bih=715&prmdo=1&tbs=frm:1&ei=1attTe7qO4jKvQOszpzbBA&start=10&sa=N) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting如果我将链接中的 hl=zh-CN& 去掉就OK了 请问 为什么?

解决方案 »

  1.   

    经过测试,
    hl=可以是任何值 如:hk,tw,us,xxx,zh-,zh-tw......
    就是不能zh-c+n+难道google屏蔽了zh-cn??但浏览器里是可以执行查询的。为什么file_get_connects不能呢?等高人解释
      

  2.   

    www.google.com.hk搜索结果页面是big5编码header("Content-type:text/html;charset=big5");//加个头$url = "http://www.google.com.hk/search?q=%E5%9B%BD%E5%AE%B6%E7%94%B5%E7%BD%91"; //直接跟关键词即可,没必要复制url链接。
    //$url = 'http://www.baidu.com';
    echo '##1';
    $page =  file_get_contents($url);
    var_dump($page);
      

  3.   

    不用什么高人,最近拿GG来测试,不定时会得到无法预料的结果,换别的试吧,你的第一个url我这边5次返回404,第六次才看到内容,我还猜是不是被你耍了
      

  4.   

    不仅仅是谷歌会出现这种问题,有些网站也会,可能跟PHP本身有关,会不会是HTTP头信息的问题?
      

  5.   

    经过测试,发现是cookie的问题当缺少PREF=ID=9e9d73d95b9fc374:U=2e41cc6df9b2c3f9:FF=1:LD=zh-CN:NW=1:TM=1297849526:LM=1299143589:S=7WlOsippv0T5wKd7; 这个cookie时,访问带有hl=zh-CN时,就会有问题,浏览器cookie禁用时表现一致
      

  6.   

    一定要file_get_contents吗,用curl吧
      

  7.   

    simple_html_dom.php,这个可以,网上搜一下吧,教程很简单。
      

  8.   

    还没结贴啊,我做个实例吧
    本地验证通过header("Content-type:text/html;charset=gbk");
    $surl = 'http://www.google.com.hk/';
    $url = "http://www.google.com.hk/search?q=%E5%9B%BD%E5%AE%B6%E7%94%B5%E7%BD%91&hl=zh-CN&newwindow=1&safe=strict&biw=1419&bih=715&prmdo=1&tbs=frm:1&ei=1attTe7qO4jKvQOszpzbBA&start=10&sa=N";$cookie_file = dirname(__FILE__)."/cookie.txt";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $surl);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_close($ch);$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $surl);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    $contents = curl_exec($ch);
    curl_close($ch);
    echo $contents ;
      

  9.   

    curl_setopt($ch, CURLOPT_REFERER, $surl);
    可以去掉
      

  10.   

    我晕,我说怎么看你代码都没啥问题,可是就是302呢?
    原来是你取cookie时没有执行。。header("Content-type:text/html;charset=gbk");
    $surl = 'http://www.google.com.hk/';
    $url = "http://www.google.com.hk/search?q=%E5%9B%BD%E5%AE%B6%E7%94%B5%E7%BD%91&hl=zh-CN&newwindow=1&safe=strict&biw=1419&bih=715&prmdo=1&tbs=frm:1&ei=1attTe7qO4jKvQOszpzbBA&start=10&sa=N";$cookie_file = dirname(__FILE__)."/cookie.txt";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $surl);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_exec($ch);//少了这句。。
    curl_close($ch);$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $surl);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    $contents = curl_exec($ch);
    curl_close($ch);
    echo $contents ;
      

  11.   


    是的,原码在本地执行的时候考虑了伪造浏览器,header输出,允许跳转等设置,
    调试的时候发现,这些有没有都无所谓,就删除了。哪知道大意把curl_exec($ch);也删了。呵呵 谢谢你的补充。
      

  12.   

    学习了。不过这里测试只需要这段代码就可以了啊。header("Content-type:text/html;charset=gbk");
    $surl = 'http://www.google.com.hk/';
    $url = "http://www.google.com.hk/search?q=%E5%9B%BD%E5%AE%B6%E7%94%B5%E7%BD%91&hl=zh-CN&newwindow=1&safe=strict&biw=1419&bih=715&prmdo=1&tbs=frm:1&ei=1attTe7qO4jKvQOszpzbBA&start=10&sa=N";
    $cookie_file = dirname(__FILE__) . "/cookie.txt";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); //设置要获取的url
    curl_setopt($ch, CURLOPT_REFERER, $surl); //设置向前连接
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //执行后返回cookie
    $contents = curl_exec($ch);
    curl_close($ch);
    echo $contents;
      

  13.   

    我想 你一定是先用的完整代码,测试成功,然后删除了第一段代码,发现了这个结果。为什么呢?? 因为在用完整代码的时候,已经把cookie抓下来保存在了cookie.txt中了,所以才有你只用第二段代码就能成功的现象,因为COOKIEFILE能读取cookie.txt的值。