Load
XML from External Domains
by
Kalliban : 13 May 2004
If you ever have tried to load an XML-file that is
located on a different domain than your Flash movie is, you
have most likely noticed that the XML.load() function is
restricted to XML-file that are located on the same domain
as the Flash movie.
This means that you cannot load the XML file
"http://www.site.com/data.xml" into the Flash movie
"http://www.swfdomain.com/flash.swf" with the XML.load()
function.
However, there is a very
simple way around this with PHP. Before going through this
tutorial, I recommend you to check out the other XML
tutorials here at kirupa.com.
Okay, let's get started!
Creating the XML
The XML-file we are going to use in this example is going to
be very basic, since it's just for demonstration. Although,
there is one thing that must not be included in the
XML-file! That is the XML declaration tag, which looks
something like this:
<?xml
version="1.0" encoding="iso-8859-1"?>
If that tag is
included in the XML-file, it will cause a PHP parse error,
since php will treat everything within the <? and ?> as
PHP-code. I will discuss this later on.
Okay, so the XML-file will be this:
<testNode>
<testChildnode testVar="blah">Test</testChildNode>
</testNode>
Just copy &
paste this into Notepad, Dreamweaver or any other program
capable of editing XML.
Save the file as
test.xml.
Creating the PHP
The PHP-file will also be very simple. We will use the
include() function to load the XML-file into the PHP-script.
<?php
include($_GET['xmlsource']);
?>
To make it a
little bit more dynamic, we will assign the path to the
XML-file using the GET method. So when we load the PHP from
the Flash movie, it will look like this:
loadXML.php?xmlsource=test.xml
Save the
PHP-script as loadXML.php.
|
XML Declaration Tag
|
As I
said earlier, we cannot write the XML declaration
tag in the XML file, but we can write it in the PHP
file, before we include the XML. If you want to use
the XML declaration tag, do like this:
<?php
echo('<?xml
version="1.0" encoding="iso-8859-1"?>');
include($_GET['xmlsource']);
?> |
|
Creating the
Flash movie
Again, this will be just a matter of copying and pasting.
These are the actions that are required to make this work:
(remove the linebreak on line two)
example_xml.ignoreWhite = true;
example_xml.load("http://www.azure.se/loadXML.php? xmlsource=test.xml");
example_xml.onLoad = function (success) {
if (success) {
trace(example_xml);
}
};
As you can see in the load()
function, we set the GET-variable xmlsource (that is used in
the PHP-script) to be the same as the name of the XML-file.
In this example, I use a PHP file that is located on the
same domain as the XML-file.
If you want to have the PHP-file on the same domain as your
Flash movie, you can do like this instead:
(again, remove the linebreak)
example_xml.load("loadXML.php?
xmlsource=http://www.azure.se/test.xml");
Save the Flash movie as
xmlExample.fla, and you're set!
Now it's just for you to
upload the files to the webserver(s) and to testrun
everything.
But be aware of that the trace() function in the Flash movie
will not display anything if you are running the SWF file in
a HTML document or in the standalone player.
|
Extra
Information |
Alternate,
Secure Method - by njs12345
Using the example PHP script provided
in the tutorial, it is possible to include any file.
For instance, under Unix, it is possible to include
the file /etc/passwd, which contains passwords for
the whole system.
This could also be used to include the source code
of other PHP documents. For instance, how would you
like it if someone included your PHP connection
strings?
It would be much better to check if the URL string
begins with "http://" using some code like this:
<?php
if(substr($_GET['xmlsource'],
0,
7)
== "http://"){
//is using http://
protocol, allowed
include($_GET['xmlsource']);
}else {
print "ERROR: not
authorized";
}
?>
- njs12345
Loading XML
From External Domains - by Marcito
If you are having difficulties trying to load the
XML file from an external domain, the following two
links should help:
- marcito
|
|
Source
If you have any thoughts or
questions about this tutorial, or about anything else for
that matter, feel free to post in kirupaForum! And don't
forget to search before posting, your question might already
have been answered!
Good Luck and Happy Flashing!
:D
Kalliban
|