Posted on Thursday 29th of November 2007 at 06:31 in Tutorials

Google Custom Search tutorial (using XML and PHP)

Recently I've been doing some work with the new Google Custom Search and while it is still in beta, it's still a very powerful tool. The main problem I had with it was the lack of useful examples/documentation explaining how to extract your results. So after some tinkering I've got a very basic example with a PHP class to make life easier.

Google Custom Search is pretty useful; for $100 a year you can have a fully customisable search engine indexing up to 5,000 pages. You are given a unique ID number, you specify the URLs that are to be searched and the results are returned as XML. Fortunately Google do provide documentation for the XML schema - because you DO need it. The XML has dozens of tiers without meaningful naming conventions... So I'm publishing my initial efforts as a tutorial to allow people to get up to speed quickly.

My googleCustomSearch PHP Class It seemed logical to write a re-usable PHP class for future implementations, so let me explain what I've done (don't worry, there is a full downloadable version):

Download the Google Custom Search example

So I start my class and have the empty googleCustomSearch() constructor. I then have a handy method for cleaning inputs (from a search form for example), making them largely safe for PHP.

The above function is to get the XML result back using the PHP SimpleXMLElement library. The $cseNumber variable is the unique identifier that Google provides you with to identify your custom search engine above anyone elses. I've spoofed this example with a random number to show you what you should expect to see. Then it's just a case of building the string for the XML file. Google provides this information but it's important to query-string on the searchterms ($searchterms) in the above way (identified by "q" in the querystring itself). The $start variable is used for pagination, I'll explain more about that later on.

getTotalResults() gives you the estimated number of total results for the search (although this is criminally inaccurate on anything but the last pages of the resultset), so this too is used in pagination. The getPageTotal() function returns how many results you're dealing with on the current page.

Here is my function that writes the pagination at the foot of the search results. This takes a number of variables as parameters to work out whether there are more results and which page you're on (so you don't write "previous page" on the first page of results, for example).

This is the important function - it outputs the search results. Obviously this will need to be customised depending on how you want your results to be displayed but this simply displays them in the same manner Google does.

And here we have the final function that writes any suggestions Google has - i.e. if you typed something incorrectly or it didn't recognise the search term. Then it will write the suggestion with the suggestion as a link which runs the search with that word. Finally we close the class.

That's all very confusing and long winded, so how do we implement it? I'm not going to explain that all here but I'll provide it as a downloadable example. It's merely a case of instantiating the class and capturing the search terms, with attention placed on the querystrings used to paginate through the results.

So basically you need to be able to take numerous querystring values to allow you to alter the starting point of the search results. This is all explained fully in comments within the PHP documents so please download them and have a look through.

Download the Google Custom Search example

Hopefully the explanation of the class is of some use, but really the best information is contained within the comments in my examples. You should be able to just replace the cseNumber and use my example. If you have any problems, questions, comments or queries please drop them in a comment or send me an email.


Enjoy this article? Why not subscribe to the full RSS feed?

blog comments powered by Disqus
Who is Seopher?

This is me. I'm a 27 year old web
developer, blogger and entrepreneur
from near London.

I've done work for people like
Samsung, Vauxhall, Cadburys,
Chevrolet, Center Parcs and TKMaxx.

I've been running this blog since 2006
and have reached more than
1.7 million readers

I'm passionate about the web, heavy metal, zombies and cats.

Subscribe to the RSS Feed

Stay up to date with by subscribing to the RSS feed, either in your browser or subscribe via email using the form below

Updates by Email

By subscribing by email you’re also subscribing to the newsletter; a periodical email outlining new reviews, competitions and other subscriber-only content

  • ReviewMeReviewMe
  • buy 125x125 advert for $50 pcm
Sponsored Links
Want to give your product/website exposure?

Paying for a featured review is a great way to give your product, service or website exposure. For as little as $75 you can have a full review on the site forever.

Advertising Bundle! Review + Banner = $100

Buy a review and get a 125x125 advert half price. Your banner gets displayed on over 542 pages for a full month.