Program Yahoo! with ColdFusion

ColdFusion MX includes all of the tools necessary to work with the Yahoo! API.

ColdFusion is a development platform for creating web applications. Its tag based template structure is a popular choice for HTML developers who want to move to more dynamic content and are already familiar with using tags. In fact, if you were to glance at a ColdFusion script, you might think the code was standard HTML. Putting together a ColdFusion template is a lot like putting together an HTML page, but you can draw on resources such as databases and web services to bring in dynamic content.

You’ll need to be running a version of ColdFusion MX or later to use this hack, because it relies on the XmlParse function that was added with the MX release. This function can take an XML document, such as the responses from Yahoo! Search Web Services, and turn it into an object that Cold-Fusion scripts can work with.

The Code

This hack shows how you can quickly use the ColdFusion Markup Language (CFML) to bring in content from Yahoo! Search Web Services. This script assembles the proper request URL based on a querystring variable and gets a response from Yahoo! with the <cfhttp> tag. Then the XmlParse function makes the data available to the script, and the <cfloop> tag goes through each bit of data, adding it to the page.

Save the following code to a file called yahoo_search.cfm and upload it to your server:

<!---
yahoo_search.cfm
Accepts a search term and shows the top results.
Usage: yahoo_search.cfm?p=<Query>

You can create an AppID, and read the full documentation
for Yahoo! Web Services at http://developer.yahoo.net/
---<
<html>
<body>
<h2>Yahoo! Search Results</h2>
<ol>
<!--- Set your unique Yahoo! Application ID --->
<cfset appID = "YahooTest">

<!--- Grab the incoming search query --->
<cfset query = "#URL.p#">

<!--- Construct a Yahoo! Search Query with only required options --->
<cfset req_url = "http://api.search.yahoo.com/">
<cfset req_url = req_url & "WebSearchService/V1/webSearch?">
<cfset req_url = req_url & "appid=#appID#">
<cfset req_url = req_url & "&query=#query#">
<cfset req_url = req_url & "&language=en">

<!--- Make Request --->
<cfhttp url="#req_url#" method="GET" charset="utf-8">
	<cfhttpparam type="Header" name="charset" value="utf-8" />
</cfhttp>

<!--- Parse Response --->
<cfset response = #XMLParse(cfhttp.fileContent)#>
<cfset results = #response.ResultSet.Result#>

<!--- Loop Through Response --->
<cfoutput>
<cfloop from="1" to="#ArrayLen(results)#" index="i">
	<li><div style="margin-bottom:15px;"> 
	<a href=\"#results[i].ClickUrl.xmlText#\">#results[i].Title.xmlText
#</a>	#results[i].Summary.xmlText#<br /> 
	<cite>#results[i].Url.xmlText#</cite></div></li>
</cfloop>
</cfoutput>
</ol>
-- Results Powered by Yahoo!
</body>
</html>

Take a look at the <cfhttp> tag in the script. Note that the charset attribute is utf-8 and that a <cfhttpparam> tag was used to set a charset header for the request with the utf-8 value. This is a bit of extra work, but it’s necessary to make sure ColdFusion and Yahoo! Search Web Services are speaking the same language.

Running the Hack

Bring up the page in a browser to see it in action:

http://example.com/yahoo_search.cfm?p=insert word

Separate multiple words with URL-encoded spaces, as in this search for " ColdFusion MX“:

http://example.com/yahoo_search.cfm?p=ColdFusion%20MX

You should see the top Yahoo! Search results for ColdFusion MX, as shown in Figure 4-8.

Yahoo! Search results for “ColdFusion MX”

Figure 4-8. Yahoo! Search results for “ColdFusion MX”

As you can see, ColdFusion MX includes all of the tools you need to make Yahoo! Search Web Services requests and work with the responses. Integrating Yahoo! data with existing ColdFusion applications can be accomplished with just a few lines of code.

Get Yahoo! Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.