$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了 请问 为什么?
hl=可以是任何值 如:hk,tw,us,xxx,zh-,zh-tw......
就是不能zh-c+n+难道google屏蔽了zh-cn??但浏览器里是可以执行查询的。为什么file_get_connects不能呢?等高人解释
//$url = 'http://www.baidu.com';
echo '##1';
$page = file_get_contents($url);
var_dump($page);
本地验证通过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 ;
可以去掉
原来是你取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 ;
是的,原码在本地执行的时候考虑了伪造浏览器,header输出,允许跳转等设置,
调试的时候发现,这些有没有都无所谓,就删除了。哪知道大意把curl_exec($ch);也删了。呵呵 谢谢你的补充。
$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;