Saturday, March 31, 2007

crossdomain.xml

Lately, I've run into a couple problems trying to work with Flash across domains. In one case another domain was trying to pull information from my server and couldn't. In the other case, I was trying to pull info from another domain (an rss feed specifically) and couldn't get to it. These problems arise from XML.load, XML.sendAndLoad, LoadVars.load, and LoadVars.sendAndLoad calls to files on other domains.

In the first case, I solved it by adding a cross domain policy on the web server. The cross domain file looks like this:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="www.domain.com" />
// repeat as needed
</cross-domain-policy>

Name that file 'crossdomain.xml' and place it in the root of your web server, and it allows each listed domain to pull from the server into any .swf that calls php scripts or xml files or whatever.

For more information on crossdomain policies, check out Adobe's Tech Note.

In the second situation, I didn't have access to the server I was getting info from. Safari's Activity Window was telling me that Flash was looking for and not finding 'crossdomain.xml,' so I was afraid I was out of luck, short of asking the webmaster of the feed to create the file and add my domain to it (which is prob unlikely to happen). However, I figured out I could create a file on my server to act as a proxy for that file, thanks to another Adobe Tech Note. I used the Server-Side Proxy method.

In that method, you create a php script which reads the file on the remote server and "recreates" it on your own, like so:

<?php
$dataURL = "http://www.domain.com/feed.rss";
readfile($dataURL);
?>

This file reads and ouputs the exact same file. So I saved that file as 'rssProxy.php' and put it on my server. Now 'http://www.myserver.com/rssProxy.php' is exactly the same page as 'http://www.domain.com/feed.rss' and I can call that file into XML.load.

So there's my solutions to both cross-domain problems that Flash has.

No comments: