RESTful Ajax in ColdFusion

on

While I was hacking and cursing my way through a routine to convert RSS feeds in to HTML … I had an idea. I thought, “Wow, maybe there’s another way to do this …” Converting RSS can be messy so I opted for a much easier and cleaner solution … JSON. For those Ajax pros out there … have a heart … I’m definitely a nOOb at JSON … I just wanted to share my experience because the introduction and experience was enlightening … at least to me it was.

Before I go in to this, I am offering this obligatory warning. What I’m about to do may not be in compliance with Google’s T.O.S. So, if you get yourself in a pickle with the big G … It’s on you. You’ve been warned.

What I wanted to do is create dynamic and fresh content for say … Oh the news. That’s a no brainer. Typically you could just grab some RSS feeds and embed them in your content. So, What if you wanted the HTML from say … Google’s news … Hmmm? You could use a SOAP request to Google for the info … But SOAP won’t be supported by Google for much longer … So, that’s no good. That’s where Google’s RESTful JSON interface comes in. According to Google documentation for their AJAX Search API, The interface was created for developers that need to have access to Google’s Search API in non-JavaScript environments. The docs provide the base URL’s to retrieve results for several of their searchers. Here’ s a list of all the types of searchers you can access remotely.

  • Web Search: http://ajax.googleapis.com/ajax/services/search/web
  • Local Search: http://ajax.googleapis.com/ajax/services/search/local
  • Video Search: http://ajax.googleapis.com/ajax/services/search/video
  • Blog Search: http://ajax.googleapis.com/ajax/services/search/blogs
  • News Search: http://ajax.googleapis.com/ajax/services/search/news
  • Book Search: http://ajax.googleapis.com/ajax/services/search/books
  • Image Search: http://ajax.googleapis.com/ajax/services/search/images
  • Patent Search: http://ajax.googleapis.com/ajax/services/search/patent

So I decided to have my hand at grabbing some news … Here we go …

<!--- Create a Couple Vars for the Search Params ... --->
<cfset gQuery = #ReReplaceNoCase("ColdFusion","\s+","%20","ALL")#>
<cfset qRegion = #ReReplaceNoCase("Fort Lauderdale","\s+","%20","ALL")#>
<!--- Call Google's AJAX Service --->
<cfset gData = "http://ajax.googleapis.com/ajax/services/search/news?v=1.0&rsz=large&#qRegion#&q=#gQuery#">

<!--- Save the Result to a Var --->
<cfhttp
url="#gData#"
method="get"
result="gDataResult">
</cfhttp>

<!--- Make the Data is JSON --->
<cfset gData = #SerializeJSON(gDataResult,false)#>

<!--- Clean up the Result Data with RegEx --->
<cfset gData = #REReplace(gDataResult.FileContent,
"^\s*[[:word:]]*\s*\(\s*","")#>
<cfset gData = #REReplace(gData, "\s*\)\s*$", "")#>

<!--- Make Sure We Have JSON ... --->
<cfif !IsJSON(gData)>
<h3>Uh Oh ... Somthing Went Terribly Wrong ... <br />
But! Don't Fret ... Coders are Hard at Work to Get Things Up ... <br />
Right Away ...
</h3>

<!--- If the JSON is Good, Deserialize It. --->
<cfelse>
<cfset gData = #DeserializeJSON(gData)#>

<!--- Create a Var for the Reponse Data&nbsp;&nbsp;&nbsp;&nbsp;--->
<cfset response = #gData.responseData.results#>
<cfloop from="1" to="#ArrayLen(response)#" index="ndx">
<cfoutput>
<p><strong>
<a href="#response[ndx].unescapedurl#" rel="nofollow"
target="_blank">#response[ndx].titleNoFormatting#</a>
</strong><br /><br/>
#response[ndx].content# <br /> <br />
</p>
</cfoutput>
</cfloop>
</cfif>

The Google Search API’s Class reference has a list of all the arguments you can include as URL parameters that will give you plenty of options to customize your results …

The following two tabs change content below.
Edward J. Beckett is a passionate software engineer, web developer, server administrator and polyglot programmer with nearly a decade experience building desktop and web applications ranging from simple personal web sites to enterprise level applications on many technology stacks including Java, Java EE, Spring, Spring MVC, Spring Data, Hibernate, SQL, JPA, JMS, HTML, CSS, JavaScript, ColdFusion, PHP, Node.js and more...