Wikipedia Web Service in Flex
Posted by Dennis on Mar 4, 2007 in ActionScript, Flex • 2 commentsCurrently I’m working on a Flexified version 2 of my guitarscales app. One of the new features is a short description of the chosen scale. Because I’m too lazy to write all these descriptions myself and because Wikipedia already has a lot of them, I decided to look around for a webservice to do this job for me.
The first place to start is ofcourse Wikipedia itself. Unfortunately, they do not have such a webservice. Although I do believe they are working on one.
After some googling I finally found the FUTEF Wikipedia API. This is a RESTful webservice and returns a string containing a JSON object. Since both ActionScript and JavaScript are based on ECMAScript, I can use the returned JSON object in my Flex application.
Because of the Security sandbox restrictions I cannot call the web service directly. As a workaround I’ll call the webservice from a local PHP file:
$keyword = $_GET["query"];
$request = "http://api.futef.com/api/v1?query=".$keyword."&appid=[please contact FUTEF to request a free appid]&begin=0&end=1";
$xml = file_get_contents($request);
Then I can call this local PHP file from within my Flex application:
var service:HTTPService = new HTTPService();
service.url = "myphpfile.php?query=" + keyword;
service.addEventListener(ResultEvent.RESULT, wikipediaResultHandler);
service.send();
In the callback function I can now create a JSON object from the returned string:
private function wikipediaResultHandler(event:ResultEvent):void {
var value:Object = JSON.decode( event.result as String );
var resultText:String = value.records[0].text;
var newLinePattern:RegExp = /\n/g;
resultText = resultText.replace(newLinePattern, "");
txtDescription.htmlText = resultText + "...<br/><a href=\""+value.records[0].url+"\" target=\"_blank\">Read more</a>";
}
Adobe has released several free and open ActionScript 3.0 APIs, including the JSON API that I used in this example. You can download this API here.




What a great use of the FUTEF API!!!! I would have never imagined something like this. Thanks for allowing all to view source.
a Wikipedia Web Service API would be so cool, and take the platform on a whole new level!
There would be hundreds of applications for this, like saying a word into your cell phone and having a Text-to-Speech Processor read out all the available information about it.
Now humanity everywhere around the world - with all different kind of devices - have access to everything we know. At least everything we officially know