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.
Thanks in advance.