1 Reply Latest reply on Sep 21, 2010 12:18 PM by tochie007

    Uploading XML Packets to Server Side Script(PHP) from AIR (HTML)

    tochie007

      Hello folks,

       

      I have a need to send xml packet from my Air app to a remote server. I have searched the net for days without finding any solution to this, so this is my last resort. I did find an example on the example on Adobe help page but I couldn't get it to work for me. I'll just post the scripts below.

       

       

      ***This is the script from Adobe help

      ===================================================================

       

      var secondsUTC = new Date().time; 
      var dataXML = "<login>" 
                          + "<time>" + secondsUTC + "</time>" 
                          + "<username>Ernie</username>" 
                          + "<password>guru</password>" 
                      + "</login>"; 
      var request = new air.URLRequest("http://www.mysite.com/xml.php"); 
      request.contentType = "text/xml"; 
      request.data = dataXML; 
      request.method = air.URLRequestMethod.POST; 
      var loader = new air.URLLoader(); 

       

      ===================================================================

       

      ***I added listeners to tell me whats going on on the server

      ===================================================================

      loader.addEventListener(air.Event.COMPLETE, completeHandler);

       

      try

      {

           loader.load(request);

      }

      catch (error)

      {

           air.trace("Unable to load URL");

      }

       

      function completeHandler(event)

      {

           air.trace(event.target.data);

      }

       

      ===================================================================

       

      ***On my php script, I simply (for now) wanted to check if my script was seeing any data from AIR

      ===================================================================

      <?php

       

           echo $_REQUEST["dataXML"];

       

      ?>

       

      ******** The RESULT *********

       

      I keep getting an error message saying the variable "dataXML" was unknown or undefined. Where am I missing it? The script in AIR or the PHP script?

       

       

      Pls someone help me out! Thanks

        • 1. Re: Uploading XML Packets to Server Side Script(PHP) from AIR (HTML)
          tochie007 Level 1

          I discovered my mistake. In my php script I was trying to read the XML packet like I would read the normal POST date "name=value". This little code below has brought an end to my search. Hope it helps someone someday!

           

          ***This reads the XML Packet sent from AIR to PHP.

           

          $postText = trim(file_get_contents('php://input'));

           

          *** from here you can use PHP to play with the XML Packet which is contained in "$postText";