tags:

views:

315

answers:

2

I am trying to retrieve just the search completion shown in Google search result for any keyword. For example the search completion for

http://www.google.com/#hl=en&q=google+keyword+search

is 48,400,000 (Results 1 - 10 of about 48,400,000 for google keyword search)

I tried to use CURL but fail to retrieve the search result page.

Any help appreciated.

+1  A: 

You don't need to do anything nearly that complicated.

Just use their search API

$search = 'google keyword search';
$results = json_decode( file_get_contents( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . urlencode( $search ) ) );

echo $results->responseData->cursor->estimatedResultCount;
Peter Bailey
Ok - I'll bite. This answer is nearly a year old and suddenly a down vote? Did the API change and this answer is out of date? Help me out here you downvoter person you.
Peter Bailey
A: 

Firstly, the link you posted is incorrect, that will just get google.com, because the hash value is never part of the request URI. It should be http://www.google.com/search?hl=en&q=google+keyword+search

Second, Google blocks crawlers from accessing anything from /search with robots.txt. Whether CURL pays attention to this or not I don't know. However, it's very likely that Google blocks access from unknown sources. I guess you could try and set the user-agent to match a web browser, though that is kind of deceiving...

DisgruntledGoat
The link works. Also, curl does not pay attention to robots.txt, that's left to the client's discretion. A valid user-agent is not required to make a request, although including one will probably keep your IP from being banned a little longer.
Justin Johnson