2011-12-21 403 views
0

如何使用php curl正确地获得curl响应。我试图改变一些请求头和用户代理配置curl以获得www.google.com

GET/HTTP/1.1 
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7 
Host: www.google.com 
Accept: */* 
Accept-Encoding: gzip,deflate,sdch 

,但它不工作我得到302错误

HTTP/1.1 302 Found 
Location: http://www.google.co.in/ 
Cache-Control: private 
Content-Type: text/html; charset=UTF-8 
Set-Cookie: PREF=ID=30a99703e541807e:FF=0:TM=1324467004:LM=1324467004:S=0VlXyYEJtxKQ_Pqk; expires=Fri, 20-Dec-2013 11:30:04 GMT; path=/; domain=.google.com 
Date: Wed, 21 Dec 2011 11:30:04 GMT 
Server: gws 
Content-Length: 221 
X-XSS-Protection: 1; mode=block 
X-Frame-Options: SAMEORIGIN 

HTML输出,我得到的是

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<TITLE>302 Moved</TITLE></HEAD><BODY> 
<H1>302 Moved</H1> 
The document has moved 
<A HREF="http://www.google.co.in/">here</A>. 
</BODY></HTML> 

如何得到和使用php curl发布数据,就好像我们正在使用浏览器一样。

这里是curl_setopt

curl_setopt($ch,CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_USERAGENT, " Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch'); 
+0

302不是错误,代码优于400是错误! – RageZ 2011-12-21 11:35:34

+0

你的问题是什么? Google的回复将您重定向到他们的印度网站 - 这不是您想要的吗? – 2011-12-21 11:36:02

+0

这么蹩脚的我.. – 2011-12-21 11:38:12

回答

1
HTTP/1.1 302 Found 
Location: http://www.google.co.in/ 

告诉你去http://www.google.co.in/的内容我的PHP代码,所以你要做的另一卷曲序列存在。

+1

-1跟着位置可以让你在一次卷曲操作中做到这一点 – ghostJago 2011-12-21 12:30:48

5

设置卷曲CURLOPT_FOLLOWLOCATION选项true

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

威尔将指示卷曲遵循任何“位置:”服务器发送头。更多信息请见documentation

+0

+1不知道那个,很有帮助。 – 2011-12-21 13:38:46