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.
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);
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.
|
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 |
|
Fin Mon Jun 8, 2009 7:55 am
What are 'd', 'f' and 'q'?
Cheers |
|
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.
|
©2004 - 2009 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN