Introduction to using Google’s search API




A lot of people these days use 3rd party sites or services to gain SEO data about their site or service. A lot of these people simply do not realise just how simple it is to build some tools to gain statistics from the big search engines. In this post I plan to give you a very basic introduction on how to go about getting some search engine statistics from Google using their API.

Two things to note for this post is that I am using PHP 5 and that cURL (libcurl) is required.

If you are new to cURL or do not know what it is, its worth reading the PHP manual on the topic. A brief introduction (from the PHP site) is;

PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

Google used to have a nice and easy SOAP API for searching but have discontinued that. They suggest using the Google AJAX API, but for most folk, this seems like overkill and as with most of Googles pages, it is almost impossible to find the info required for a simple server-side implementation. A little digging though, and you arrive at this beauty.

So, the google example code is very simple;

$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.mysite.com/index.html");
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);

The Google class reference can be seen here.

This is great if you want to get the results for Paris Hilton and don’t want to display them. So, lets spruce it up a bit.
Firstly, we’ll write a simple form;

Now that we have the google API code and a very simple form, we need to get the form search data and append it to the google search URL;

if (array_key_exists($_POST['search'])) { // We need to make sure that there is something to search for first
    $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="  . urlencode($_POST['search']); // We encode the search string so that it can be used in a URL without causing problems
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.fliquidstudios.com/scripts/google_v1.php");
    $body = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($body);
}

If we add a var_dump($json) to the bottom of the script, we get a huge object output to the screen. The object contains a lot of useful data;
print($json->responseData->cursor->estimatedResultCount); // Shows us how many pages in Googles index matches our search
print($json->responseData->results); // Shows us the results of the search

Ok, so this is great but if we want to aggregate a bunch of info to display, ie how many blog posts are indexed and how many sites link to us, we will need to re-run this search several times… I dont like doing things several times, so lets create an array of search terms;

$searches = array(
"web" => "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . urlencode($_POST['search']), // Normal web search
"links" => "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . urlencode("link:" . $_POST['search']), // Normal web search for sites linking to us
"site" => "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . urlencode("site:" . $_POST['search']), // Pages on our site indexed by Google
"blog" => "http://ajax.googleapis.com/ajax/services/search/blogs?v=1.0&q=" . urlencode($_POST['search']) // Normal web search
);

Keep in mind that for this to work, you need to enter a URL.

Thats it. Its really that easy to use the Google API. I will eventually write a post on how to get the same data from the Yahoo! API and I will be building on this code and post to show how to get more info and style it properly.

Demo link

Full code

Also please note that at Fliquid, we use the Zend coding standards, so no ending ‘?>’ and 4 space indents.

  • Hi, I've been looking for something like this for a very long time, and am glad I finally found it.

    How could I get the latitude & longitude?

    Is there a way to make the results look in a specific format?

    I'd like to look something like:

    Title
    url
    content
    email
    latitude
    longitude

    thanks in advance,

    Sebastian
  • As far as google is concerned you are a regular user doing a search when using the PHP 5 DOM method described above so there should be no limit that doesn't also apply to regular users using their search.
  • Thanks a lot, I will look into that. Do you know if/how many automated searches per day or so that Google allows? Since this is not via the API, I guess they could complain of someone run a lot of searches (not that I plan to, but it's good to know the limits). I may also be interested in the Yahoo API, so I will look out for that topic here.
  • Hi Anders,

    I've put together a very quick example of how you could get this number from the actual google search results page using the PHP 5 DOM extension. You can view the example at http://www.fliquidstudios.com/projects/google-a...
  • Hi Anders,

    You could physically parse the google search results page in PHP and then use PHP's DOM functions (http://au.php.net/dom) to grab the number you want. It'd be very messy as the number of results doesn't have its own div or span.
    Michael recently did something similar, I'll see if he can shed some light on this.

    Christian
  • Good tutor. But, I'm disappointed in the google search api, because the values returned by estimatedResultCount are very inaccurate. They differ a LOT compared to a regular google search. Try some example searches and you will see what I mean.
    Obviously Google are aware of the problem, since may...
    http://code.google.com/p/google-ajax-apis/issue...

    Discussions on the topic
    http://groups.google.co.in/group/Google-AJAX-Se...
    http://groups.google.com/group/Google-AJAX-Sear...
    http://groups.google.com/group/Google-AJAX-Sear...

    Does anybody know of an other way to get the number of google results for a specific word? (I do not have a SOAP API key)
  • Hi Edward,

    Thanks for the comments. Google's pages are hard to navigate sometimes.
    I'll be doing more with this API and the Yahoo! API on here very soon also.

    Thanks again,

    Christian
  • Great article and even better discovery! ... I was searching Google's search API for the very same reason as you ...

    I'll definitely get some mileage from this ...

    Thanks Christian
  • Thanks Juan! Wont be long.
  • Looking forward to the next article in you series.
blog comments powered by Disqus