Google Ajax API is a neat way to create your own little search function inside your webpage, using Ajax. But with few lines of codes, we can do the same inside ColdFusion too.
In this example, I'm going to search within this site. So I went to Google advance search and build up my query string for the keyword "coldfusion". It turn out to be something like [coldfusion site:cflove.org]
Now all we have to do is, call Google Ajax API using CFHTTP, like this:
But there is a one little trick to this, since this is officially an Ajax API, Google want to know where the request originated from. So we have to pass a cgi.rederer with our request. It is quite easy to do, thanks to the little fellow "cfhttpparam". Since I'm at it, I also changed the UserAgent to IE8, to make it look little more like a typical browser request.
This returns search results as a JSON structure. If you are using CF8, you are lucky, you can use function DeserializeJSON(), but if not, still there is the most wonderful cfjson tag you can download here.
Google Ajax API has a limit of 8 pages of search results and maximum 8 results per a page. You can fetch results page-by-page or do what I did bellow, loop around all pages, and fetch everthing at once.