Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Loading remote XML in Flash 8

Loading remote XML in Flash 8

Loading remote XML in Flash 8 for local usage. A common task in development.

Flash player 8 introduced a new way for loading content into Flash. One crucial change was a local movie can no longer load data from a remote server without adding permissions on the server. Well what happens if you can't access the server to add them? Here is what you do.

Note we are assuming that you have a local webserver running and our PHP file will be stored in the home directory of the local web server.

Here is a standard XML call in Flash, nothing advanced going on here.

var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function(success:Boolean) {
	if(success) {
		trace("XML: " + this);

The above call will fail with an "ERROR OPENING URL" message due to the sandbox restrictions.

However we can work around this using the power of PHP and a common technique in programming called a "passthru". In the end we achieve the goal of loading XML for Flash. Here is the PHP needed.

$domainToLoad = $_GET['d'];
$fileToLoad = $_GET['f'];
$queryParams = $_GET['q'];
readfile($domainToLoad . $fileToLoad . $queryParams);

  • $domainToLoad The domain that will be called
  • $fileToLoad The file to load, this would be the file that generates your XML.
  • $queryParams The parameters to send with the $fileToLoad
  • Finally you make a call to readfile() passing in those three pieces of information.

Now let's tweak our "standard" XML call using our new passthru script

var domain:String = "";
var file:String = "phpfile.php";
var passThruScript:String = "http://www.localhost/passthruscript.php";
var query:String = "param1=value1¶m2=value2";

var xml:XML = new XML();
xml.ignoreWhite = true;
xml.onLoad = function(success:Boolean) {
	if(success) {
		trace("XML: " + this);
xml.load(passThruScript + 
	"?d=" + domain + 
	"&f=" + file +
	"&q=" + escape(query)

First part is the domain to load, then the file to call. In this case it is a PHP file that serves up XML. Next up is our pass thru script and finally we have our query params.

Now regardless of how the policy file is setup for remote loading you will be able to get the content you require.

In my work I took this script to the next level and created a class that handles all of this, makes testing easier and saves bandwidth.

if you have any questions, please feel free to ask.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Loading remote XML in Flash 8

 Damian  Sun Jun 1, 2008 12:39 am  
Hey, thanks for this example. I found a slight bug in it though...

The bit of PHP needs to have the query '?' included like so:

readfile($domainToLoad . $fileToLoad . "?" .$queryParams);

(either that, or include it along with the escaped parameters)
 Fernando  Sun Jun 1, 2008 11:24 am  
Sorry, could you explain a little better with the address i need? I'm trying to put a feed into flash. Here is the remote address(,,HME0-9585,00.xml)
I didn't undertand the phpfile.php. Do i need a php with the nods?
 Fin  Mon Jun 8, 2009 7:55 am  
What are 'd', 'f' and 'q'?

 mkeefe  Fri Jun 26, 2009 11:56 pm  
@Fin - Those are the query string parameter names, you could make them anything. Just make sure you change the Flash and PHP code, if you do.
 EW  Wed Jun 9, 2010 1:04 pm  
Hey not sure I completely understand how this works. Are we working in three different files? meaning swf for actionscript, .php for php, and an xml file? So all three are communicating?
 mkeefe  Wed Jun 30, 2010 11:22 am  
@EW - Yes, there is an FLA (Flash File), AS (Actionscript), PHP (server code) and the XML is what you are trying to load.
 ignacio  Wed Nov 10, 2010 12:38 pm  
It works great!!!!! thanks you!!!!!
 mkeefe  Wed Nov 17, 2010 1:55 pm  
@ignacio - Glad to hear, happy coding.
Add a comment
The ability to add new comments has been disabled in this archived version.

Main | Tutorials | Articles