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 = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "");
$body = curl_exec($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 = ""  . 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, "");
    $body = curl_exec($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" => "" . urlencode($_POST['search']), // Normal web search
"links" => "" . urlencode("link:" . $_POST['search']), // Normal web search for sites linking to us
"site" => "" . urlencode("site:" . $_POST['search']), // Pages on our site indexed by Google
"blog" => "" . 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.

  • Silinx_1_1

    it does not work

  • That is really nice information. I will surely look into that code and will see if that works or not.

  • Interesting ideas!I will put them in application!

  • Very good blog, thanks for sharing it .

  • David

    Hi ,
    the demo link does not properly function .. it references the word null and then when I try the search I get a blank back..

blog comments powered by Disqus