Google AJAX Search API with Coldfusion

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:

Show/Hide Line Numbers . Full Screen . Plain

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.

   1: <cfhttp
   2:         url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=coldfusion&site:cflove.org&start=1&rsz=large&hl?=en"
   3:         userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.3072">
   4:         <cfhttpparam encoded="no" type="CGI" name="referer" value="http://cflove.org/examples/other/google.cfm" />
   5: </cfhttp>
Show/Hide Line Numbers . Full Screen . Plain

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.

   1: <cfset q    = 'coldfusion site:cflove.org'>
   2: <cfset start = 0>
   3: <cfloop from="1" to="20" index="i">
   4:     <cfhttp
   5:         url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=#q#&start=#start#&rsz=large&hl?=en"
   6:         userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.3072">
   7:         <cfhttpparam encoded="no" type="CGI" name="referer" value="http://cflove.org/examples/other/google.cfm" />
   8:     </cfhttp>
   9:     <cfset out = DeserializeJSON(CFHTTP.FileContent)>
  10:     
  11:     <cfif IsDefined('out.responseData.results')>
  12:      <cfset start = start+ ArrayLen(out.responseData.results)>
  13:         <cfloop array="#out.responseData.results#" index="r">
  14:             <cfdump var="#r#">
  15:         </cfloop>
  16:     <cfelse>
  17:      <cfbreak>
  18:     </cfif>
  19: </cfloop>
Show/Hide Line Numbers . Full Screen . Plain

Name  
(required)
Email  
(required - never shown publicly)
Web Site  
Notify me of new comments via email.
Notify me of replies via email.
1 Comment :
Francesco
on Friday 17 February 2012 08:15 PM
How could I found if a value is inside the response?