ActionScript Yahoo! Search API - 3 Simple Steps
3 simple steps to performing a Yahoo! search in Flash
Download the YahooAPI class and examples
Yahoo! Developer Network provides developers a diverse offering of free web services. These services enable you to easily integrate feature rich functionality (e.g. web search, spellcheck, and contextual term extraction) with virtually no overhead other than loading an XML document via the native XML object (ActionScript 2) or URLLoader object (ActionScript 3).
Additionally, Yahoo! web services provide the developer an enormous pool of internet accessible resources, such as photos, videos, mp3’s, and data which can add complex dimensions to the already engaging Flash user experience.
These resources, and the developer’s ability to construct with them, can illustrate the multiplicity of advantages offered by applications created with Flash as opposed to more conventional client-side web development paradigms.
This article and the code provided will show you how to easily implement any of the Yahoo! search web services in ActionScript 2 or 3. The scope of this article is simply to demonstrate a sound approach to querying the Yahoo! search REST API from within Flash. It is very easy.
3 simple steps to performing a Yahoo! search in Flash
1: Get an application id
First, in order to use the Yahoo! Search web services, you will need an application id (get one here). Both the ActionScript 2 and ActionScript? 3 classes use the same syntax for specifying the id.
Once you have an application id, the first thing your application will need to do is register the id with the YahooAPI class, as follows:
YahooAPI.APPLICATION_ID = ‘lkj34988fgjh2398ife923j4kjk234’;
2: Setup an Event Handler
Next, you need to set up an handler Function which will be invoked when the search response is received, as follows:
function onYahooSearchResult(e:com.yahoo.search.YahooSearchResultEvent):Void
{
trace(‘onYahooSearchResult invoked’);
trace(e.length + “ items found: \n”;
var result:YahooSearchResult;
for(var i:Number = 0; i < e.length; i++)
{
result = e.getResult(i);
trace(result.Title);
trace(result.Url + “\n”);
}
}
3: Search!
Once you’ve registered your application id, and created the search result event handler, all you need to do is invoke the static search method on the YahooAPI class, as follows.
Note: It is worth mentioning that the signature for the search method on the YahooAPI class differs between the ActionScript 2 implementation and the ActionScript 3 implementation.
The difference is due in large part to syntactical differences between the two languages and an effort on our part to remain consistent with the existing REST API implementations published by Adobe (com.adobe.webapis.*).
In short, the ActionScript 3 Event model works much better than the ActionScript 2 model. In addition, we take advantage of new functionality, such as the URLLoader class available in ActionScript 3.
ActionScript 2
// This example performs a web search for “monkey” which will return a maximum of 25 results:
YahooAPI.search(this, ‘monkey’, YahooAPI.WEB_SEARCH, 25);
ActionScript 3
// This example performs a web search for “monkey” which will return a maximum of 25 results:
YahooAPI.search(‘monkey’, YahooAPI.WEB_SEARCH, new NameValuePair('results', 25));
// The above call returns a URLLoader. You then subscribe to that objects events as follows:
yahooLoader.addEventListener(YahooAPI.ON_SEARCH_RESULT, onYahooSearchResult);
Specifying additional parameters
Of course you can perform different types of searches, and pass additional search parameters.
The following examples demonstrate doing so:
ActionScript 2
// image search w/ 25 results
YahooAPI.search(this, ‘monkey’, YahooAPI.IMAGE_SEARCH, 25);
// news search, with options (25 results, limited to news.yahoo.com)
YahooAPI.search(this, ‘monkey’, YahooAPI.NEWS_SEARCH, 25, ‘&site=news.yahoo.com’);
ActionScript 3
// news search, with options (25 results, limited to news.yahoo.com)
YahooAPI.search(‘monkey’, YahooAPI.NEWS_SEARCH, new NameValuePair('results', 25), new NameValuePair('site', 'news.yahoo.com'));
// image search w/ 25 results
YahooAPI.search(‘monkey’, YahooAPI.IMAGE_SEARCH, new NameValuePair('results', 25));
Download the YahooAPI class and examples
Yahoo! API - ActionScript 2
Yahoo! API - ActionScript 3
Summary
That’s it!


Fantastic
Comment by Alex Chong — December 30, 2006 @ 2:48 am
Excellent !
Comment by Sangram — November 13, 2007 @ 5:24 am