Scriptplayground Network

Scriptplayground Preview
Fireworks CS4 Learning Center

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. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.

The Flash and PHP Bible has a dedicated forum for support and comments.

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);
	}
}
xml.load("http://www.example.org/phpfile.php?param1=value1¶m2=value2");

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 = "http://www.example.org/";
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.

| Print It |  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(http://www.globo.com/Rss/Globo.com/Plantao_noticias/0,,HME0-9585,00.xml)
I didn't undertand the phpfile.php. Do i need a php with the nods?
Thanks
Add a comment
Name:
Website:
Comment:
Please note: Offensive comments, flaming and spamming is not permitted on this site and your comment will be deleted immediately.

HTML is not allowed.

Please provide all comments in English so that others can help you. A common helper in this is to use an online translator.

As a security measure your ip will be recorded.
 
Anti-Robot Check:

Enter the key you see above.

What is this?: This extra test has been added due to the recent explosion of spam.
 
Google