Jey
Jey

Reputation: 1511

configure curl to get www.google.com

How to get curl response properly using php curl. I tried to change some request header and user agent

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

but its not working am getting 302 error

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

the html output that i get is

<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>

how to get and post data using php curl as if we are doing it from browser.

here is my php code for 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'); 

Upvotes: 0

Views: 1882

Answers (2)

Moz Morris
Moz Morris

Reputation: 6761

Set the cURL CURLOPT_FOLLOWLOCATION option to true:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Will will instruct cURL to follow any "Location: " headers that the server sends. More info available in the documentation.

Upvotes: 5

Eugen Rieck
Eugen Rieck

Reputation: 65274

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

tells you to go to http://www.google.co.in/ for the content, so you have to do another cURL sequence there.

Upvotes: 1

Related Questions