A few months ago someone asked me if I could get Google search results to show up inside their own template, rather than having a form that left the site. After a little research, Google CSE looked like just the thing, so I got an API key and dove in.

I wasn’t very happy with the way it worked. Basically the results came back in an iframe that holds a Google page. But it was indeed getting results, so I put it in place.

I got an email a few days later asking why the results were so “off”. A quick search revealed that the term “Jesus” returned just one or two hits, even though it was used hundreds, perhaps thousands of times on the site.

More research revealed that CSE doesn’t use Google’s regular data pool, meaning that just because you can find something with Google’s regular search engine doesn’t mean that you’ll find it in your CSE. The docs seemed to indicate that I should submit an XML site map, so I found a good site that will crawl my site and generate one. I submitted it, and 4 weeks later the results were a little different but not really any better.

At that point I remembered I had a Google Hacks book, and checked it out. It turns out it was for Google’s SOAP API, which they’ve discontinued. Google has a new thing called the AJAX API. Their interface is a slick little AJAX tool, but I really didn’t like the way it looked. Then I noticed they have a section for Flash and Other Tools. That’s where it all started to come together.

Using PHP and cURL, it’s easy to send off a query and get the results back in a JSON stream, which is then easy to parse and render. I mentioned that to Dave Brondsema and he said “Oh, I started on that a while back, and made great progress, but got pulled away. You want what I have?” Score.

So I took what Dave had and made it into a couple different php functions for easy calling. I still need to clean it up some, and comment it better, but it works marvelously.

2 thoughts on “Google Custom Search Engine

  1. That’s interesting!

    I just started working on a search page of my own for captioned videos, using get_file…

    Since most sites like google don’t have a way to limit a search to captioned video, I am linking to sites that have captioning, and to vodpod, where you can collect the more random collection.

    I pull the content, explode the body of the content that I want, explode the individual results, and strip the html tags except and

Leave a Reply

Your email address will not be published. Required fields are marked *