如何使用php curl正确获取卷曲响应。我试图更改一些请求标头和用户代理
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 :(得分:5)
将cURL CURLOPT_FOLLOWLOCATION
选项设置为true
:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
将指示cURL遵循服务器发送的任何“Location:”标头。有关详情,请参阅documentation。
答案 1 :(得分:1)
HTTP/1.1 302 Found
Location: http://www.google.co.in/
告诉您转到http://www.google.co.in/获取内容,因此您必须在那里执行另一个cURL序列。