views:

53

answers:

2

Hi

I am trying to load an xml file from wikipedia into my flash movie.

loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, tweetLoaded);
loader.load(new URLRequest("http://en.wikipedia.org/w/api.php?action=query&rvprop=content&format=xml&pageids="+subNum));
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorFunction);

This works fine when the flash file is run locally but when I upload to my domain it does not seem to work. I have read elsewhere that the cross domain rule does not apply to XML files only to images and other media. Is this true? If not is there a work around so that I can load in XML files from domains other than the one the swf is hosted on?

thanks

EDIT:

Okay I am really confused, my program queries both Bing API and the media wiki API. The Bing api call works fine, I can retrieve the XML search results back from it fine. But the wikipedia call does not work (online). I have tried listening for the Security_Error on the wikipedia call but it does not fire.

Does anyone have any ideas? Losing it a bit.

L

+2  A: 

A workaround is setting up a proxy with some server side language, so your swf loads data from your domain. This proxy forwards the request to the real host and returns the response to the swf. From the flash side, this works transparently.

You could make your proxy more or less sofisticated, but it could be as simple as (in php):

echo file_get_contents($_GET['target_url']);

This is just to give you an idea, you might want to validate the target_url parameter.

Have your swf call this php script and pass target_url as a parameter. Something like this:

var url:String = "proxy.php";
var paramVal:String = encodeURIComponent("http://en.wikipedia.org/w/api.php?action=query&rvprop=content&format=xml&pageids="+subNum);
url += "?target_url=" + paramVal;
loader.load(new URLRequest(url));

Note that for php this will require allowing fopen for urls (similar permissions might be neccesary for other server side technologies). Also, keep in mind this will affect your server bandwith consumption.

PS

Bing works because they have a crossdomain policy file in place to allow access to swfs from other domains.

http://api.bing.net/crossdomain.xml

Wikipedia doesn't have a crossdomain policy file that grants you access from other domains, so you cannot connect directly from your swf.

Juan Pablo Califano
ahh thanks for that, it just seems weird I am also querying BING to retrieve images which I can do using a cross domain xml file is that not possible for flash?
Dreamguts
@user150946. Check out my edit regarding Bing.
Juan Pablo Califano
Ahh thanks for the clarification. Would it be possible to use Javascript / JQuery instead of PHP?
Dreamguts
@user150946. I don't know much Javascript, but it's possible. I've used this for Facebook apps (the "official" AS 3 library is a wrapper Facebook's Javascript SDK. I think iframes are used for this: http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication. Google for Javascript crossdomain and you'll find many resources on this (such as this one: http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html).
Juan Pablo Califano
+2  A: 

Hey Juan

Thanks so much for you help. In the end i used http://pipes.yahoo.com

I created a pipe that took in an ID number then spat out a JSON object with the title of the corresponding wikipedia page.

which you can use here http://pipes.yahoo.com/wikibyid

For anyone else doing this you need to make sure you access the pipe from the yahoo api URL http://pipes.yahooapis.com/ as this domain has the crossdomain.xml file.

thanks

Lach

Dreamguts
Glad you worked it out. I didn't know this service existed. Good to know. Looks very cool and it definitely could be of use. Thanks for sharing it!
Juan Pablo Califano