6 Replies Latest reply: Aug 12, 2012 6:25 AM by klinek3 RSS

    load XML file from addon domain without cross-domain Policy file

    klinek3

      Hello.

       

      Assuming that there are two addon domains on the same server: /public_html/domain1.com       and      /public_html/domain2.com

       

       

       

      I try to load XML file from domain2.com into domain1.com without using cross-domain policy file (since it doesn’t work on xml files in my case).

       

      So the idea is to use php file in order to load XML and read it back to flash.

       

      I’ve found an interesting scripts that seems to do the job but unfortunately I can't get it to work. In my opinion there is somewhere problem with AS3 part. Please take a look.

       

      Here are the AS3/PHP scripts:

       

       

       

       

       

      AS3 (.swf in www.domain1.com):

       

       

      // location of the xml that you would like to load, full http address

      var xmlLoc:String = "http://www.domain2.com/MyFile.xml";

       

      // location of the php xml grabber file, in relation to the .swf

      var phpLoc:String = "loadXML.php";

       

       

      var xml:XML;

      var loader:URLLoader = new URLLoader();

      var request:URLRequest = new URLRequest(phpLoc+"?location="+escape(xmlLoc) );

       

      loader.addEventListener(Event.COMPLETE, onXMLLoaded);

      loader.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorHandler);

       

      loader.load(request);

       

       

      function onIOErrorHandler(e:IOErrorEvent):void {

          trace("There was an error with the xml file "+e);

      }

      function onXMLLoaded(e:Event):void {

          trace("the rss feed has been loaded");

          xml = new XML(loader.data);

          // set to string, since it is passed back from php as an object

          xml = XML(xml.toString());

       

          xml_txt.text = xml;

      }

       

       

       

       

       

       

       

      PHP (loadXML.php in www.domain1.com):

       

       

      <?php

      header("Content-type: text/xml");

      $location = "";

      if(isset($_GET["location"])) {

       

          $location = $_GET["location"];

          $location = urldecode($location);

      }

       

      $xml_string = getData($location);

       

      // pass the url encoded vars back to Flash

      echo $xml_string;

       

       

       

      //cURLs a URL and returns it

      function getData($query) {

       

          // create curl resource

          $ch = curl_init();

       

          // cURL url

          curl_setopt($ch, CURLOPT_URL, $query);

       

          //Set some necessary params for using CURL

          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

       

         //Execute the curl function, and decode the returned JSON data

          $result = curl_exec($ch);

          return $result;

       

          // close curl resource to free up system resources

          curl_close($ch);

      }

      ?>

        • 1. Re: load XML file from addon domain without cross-domain Policy file
          Ned Murphy MVP

          Do you get either of the two traces from your swf functions?  If you get the one from the onXMLLoaded function, have you traced the loader.data?  If you get the one from the onIOErrorHandler function, is the swf file in the same folder as the html page that embeds it?

          • 2. Re: load XML file from addon domain without cross-domain Policy file
            klinek3 Community Member

            I get trace from onIOErrorHandler function.

             

            The swf file is in the same folder as the html page that embeds it (I've also tested .swf file without embedding)

            • 3. Re: load XML file from addon domain without cross-domain Policy file
              Ned Murphy MVP

              What is the trace you get from the onIOErrorHandler?

              • 4. Re: load XML file from addon domain without cross-domain Policy file
                klinek3 Community Member

                There was an error with the xml file [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream error. URL: http://domain1.com/loadXML.php?location=http%3A//www.domain2.com/list.xml"]

                • 5. Re: load XML file from addon domain without cross-domain Policy file
                  Ned Murphy MVP

                  I just recreated your scenario using your code as shown and the textfield displayed the xml file contents as expected. 

                   

                  You might need to look into the permissions for your php file or the settings on your server (I am assuming you are testing on a server - if not, do so).

                   

                  Sometimes the easiest way to reconcile a problem is to limit the number of variables in it that are possible contributors.  In your case, you could have the php file echo simple text instead of trying to load the xml file and see if that displays.  If the php file doesn't echo the simple text, then the problem lies in something with your php file, though I would say it is not the code in the file unless your server uses an older version of php than your code.

                  • 6. Re: load XML file from addon domain without cross-domain Policy file
                    klinek3 Community Member

                    I think you might be right about permissions/settings on the server for php. Unfortunately I'm not allowed to adjust them.

                    So I wrote my own script - this time I used file path instead of http address of the XML file.  It works fine in my case.

                     

                     

                    Here it is:

                     

                    XML file on domain2.com:

                     

                    <?xml version="1.0" encoding="UTF-8"?>

                    <gallery>

                       

                        <image imagePath="galleries/gallery_1/images/1.jpg" thumbPath="galleries/gallery_1/thumbs/1.jpg" file_name= "1"> </image>

                           

                        <image imagePath="galleries/gallery_1/images/2.jpg" thumbPath="galleries/gallery_1/thumbs/2.jpg" file_name= "2"> </image>

                     

                        <image imagePath="galleries/gallery_1/images/3.jpg" thumbPath="galleries/gallery_1/thumbs/3.jpg" file_name= "3"> </image>

                       
                    </gallery>

                     

                     

                     

                     

                     

                    swf  on domain1.com:

                     

                     

                     

                    var imagesXML:XML;

                     

                       

                    var variables:URLVariables = new URLVariables();

                     

                     

                    var varURL:URLRequest = new URLRequest("MyPHPfile.php");

                    varURL.method = URLRequestMethod.POST;

                    varURL.data = variables;

                      

                     

                    var MyLoader:URLLoader = new URLLoader;

                    MyLoader.dataFormat =URLLoaderDataFormat.VARIABLES;

                    MyLoader.addEventListener(Event.COMPLETE, XMLDone);

                     

                     

                    MyLoader.load(varURL);

                     

                     

                        

                    function XMLDone(event:Event):void {

                       

                        var imported_XML:Object = event.target.data.imported_XML;

                       

                        imagesXML = new XML(imported_XML);

                       

                       MyTextfield_1.text = imagesXML;

                       MyTextfield_2.text = imagesXML.image[0].attribute("thumbPath");  // sample reference to attribute "thumbPath" of the first element

                    }

                     

                     

                     

                     

                     

                     

                     

                    php file on domain1.com:


                    <?php

                     

                     

                     

                    $xml_file = simplexml_load_file('../../domain2.com/galleries/gallery_1/MyXMLfile.xml');  // directory to XML file on the same server

                     

                     

                    $imported_XML = $xml_file->asXML();

                     

                    print "imported_XML=" . $imported_XML;

                     

                    ?>

                     

                     

                     

                     

                     

                    Regards

                     

                     

                    PS: for those who read the above discussion: the first and the second script work but you must test which one is better in your situation. The first script will also work between two domains on different servers. No cross domain policy file needed.