I want to cURL google search result in php I want to cURL google search result in php curl curl

I want to cURL google search result in php


I was successfully able to bypass google's attempt to prevent curl search by the following:

$useragent = "Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.14912/870; U; id) Presto/2.4.15";$ch = curl_init ("");curl_setopt ($ch, CURLOPT_URL, "http://www.google.com/search?hl=en&tbo=d&site=&source=hp&q=".$query);curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); // set user agentcurl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);echo $output = curl_exec ($ch);curl_close($ch);

Note the user agent I used is an old opera mini browser. this way google displays an html content that you can parse.

THIS IS AGAINST GOOGLE TOS, please do not abuse ;)

[EDIT] use $query = urlencode($query)


In this particular instance you this won't work because Google has specifically designed this URL to not be cURL-able. You'll notice (as Quentin has noted) that the url is using an anchor string rather than standard query string syntax (the variables should come after a ? but in this case they're coming after a #). Google has a piece of javascript that grabs the anchor string and then uses ajax to load content into the results frame. file_get_content and cURL are therefore powerless to get the results from this URL.

There are other places where you can pass in proper query strings:

http://www.google.ca/search?q=query+filetype%3Apdf+site%3Ayour_domain.com&hl=en&num=10&lr=lang_en&ft=i&cr=&safe=images

And it will get fetchable, but this almost certainly violates Google's TOR, so tread with caution. Also, there is a pay-for Google service that allows you to do this easily and without any pesky threat of a lawsuit.


the other guys were right about warning you to check the TOS and about the fact that the anchor you are using in the url doesn't look right. But even if that anchor does not exist you still should get the main page. So the things that I think that might cause the problem:

are you sure that the proxy you want to use works fine? run a test without this line:

curl_setopt($ch, CURLOPT_PROXY, '192.168.0.1:1501');

also, they might make some checks that involve the user agent and you are not providing any value, so consider adding a like like:

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');