Yahoo Search API

This weekend I wanted to play a bit with Yahoo’s Search API, so I thought I’d share my results here. Yahoo has a pretty big developer network, but unfortunately they have no ColdFusion examples. (Hey, Yahoo, lets fix that!) I had a bit of trouble finding the actual API however. After a few minutes of searching I found it here: Web Search Documentation. Yahoo uses a simple REST based process. This means you can use CFHTTP and URL parameters instead of a full fledged web service. The result is a nice XML packet you can easily parse.

The next thing you need to test Yahoo’s API is an application ID. You can get one using this form: http://api.search.yahoo.com/webservices/register_application.

One last note. Yahoo’s FAQ says:

Q: Why does ColdFusion keep giving me a "Connection Failure" message?
It's an encoding issue. You need to add to your cfhttp call and it should work.

This did not work for me. Instead I used the charset attribute of the cfhttp itself. Here is a simple example. I changed the appid value though:

<style> li { margin-bottom: 10px; } </style>

<cfset searchTerm = “coldfusion”> <cfset results = “10”> <cfset appid = “billgatesisdabomb”>

<cfhttp url=”http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=#appid#&query=#urlEncodedFormat(searchTerm)#&results=#results#” result=”result” charset=”utf-8”> </cfhttp>

<cfif len(result.fileContent) and isXml(result.fileContent)>

&lt;cfset xmlResult = xmlParse(result.fileContent)&gt;

&lt;cfoutput&gt;
Your search for #searchTerm# resulted in #xmlResult.resultSet.xmlAttributes.totalResultsAvailable# matches. You returned #xmlResult.resultSet.xmlAttributes.totalResultsReturned# results.

&lt;ul&gt;
&lt;/cfoutput&gt;

&lt;cfloop index="x" from="1" to="#xmlResult.resultSet.xmlAttributes.totalResultsReturned#"&gt;
	&lt;cfset node = xmlResult.resultSet.xmlChildren[x]&gt;
	&lt;cfset title = node.title.xmlText&gt;
	&lt;cfset summary = node.summary.xmlText&gt;
	&lt;cfset iUrl = node.url.xmlText&gt;
	&lt;cfset clickurl = node.clickurl.xmlText&gt;
	
	&lt;cfoutput&gt;
	&lt;li&gt;&lt;a href="#clickurl#"&gt;#title#&lt;/a&gt;&lt;br&gt;
	#iURL#&lt;br&gt;
	#summary#
	&lt;/cfoutput&gt;
&lt;/cfloop&gt;

&lt;cfoutput&gt;
&lt;/ul&gt;
&lt;/cfoutput&gt;

</cfif> </code>

I’m not sure I even need to explain anything here. The CFHTTP call is pretty standard, again note the charset attribute. Also be sure to check the documentation for what all the URL parameters mean. The doc also explains the XML result. Honestly I just dumped it and figured it out. The only thing confusing was the “clickURL”. Yahoo wants you to use that url for html links to help them track usage of the API.

Tomorrow I’ll write up a somewhat more useful example of this API.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments