Calling XML from foreign domain

So far we have always assumed that the xml file(s) is(are) on your server. But what if it is on a different domain and you need to call it and you do not have any access to that domain? Since Flash 7 it is not possible for security reasons to call a file from a different domain directly. It works on your hardrive but not when the movie is uploaded to the server. There are several workarounds but I am showing only one here using php. The principal is that the xml file is called by the php file, which once loaded can now be loaded by the flash movie. Here is the flash movie to demonstrate that and we call an xml file from Macromedia.
This is what we need to put in our movie to call and parse the xml:
import mx.controls.Button;
import mx.controls.TextArea;
class CallUrl {
	private var myTextfield:TextArea;
	private var myBut:Button;
	private static var sendFile:LoadVars;
	private static var target_xml:XML;
    
	public function CallUrl() {
	}
	public function callItNow():Void {
	   //create a LoadVars object to communicate with the php file on the server

       sendFile = new LoadVars();
	   //create an xml object, since we are calling xml file

       target_xml = new XML();
	   target_xml.ignoreWhite = true;
	   //there is no load function, which is in the php script.
 
	   //The onload function will be initiated by the signal from the server, when the xml file is loaded.

       target_xml.onLoad = function() {
	       _root.myTextfield.text = this;
	   };
	   _root.myBut.onPress = function() {
	       //here we initiate the communication with the server.

           sendFile.sendAndLoad("proxy.php", target_xml, "POST");
	   };
	}
}                          
As you can see the scripting is slightly different from a regular xml call. Now we need to have a look at the php file (courtesy of Macromedia.com). The dataURL holds the url we are calling. The php method readfile will just drop the contents of the file so we can retrieve it. And that is all we need to do.
<?php

$dataURL = "http://www.macromedia.com/desdev/resources/macromedia_resources.xml";

//note that this will not follow redirects
readfile($dataURL);

?>     
PREVIOUS