While reading XML data into Flex is easy, sending it out for writing was
more challenging. I found an example like the below based on worked fine
for sending name/value pairs to PHP, like a form would do. I needed to
send an XML object, and since the docs say you can, I eventually found a
way.  I knew PHP was picking up the content, but I had trouble finding
how to grab it as $_POST nor $HTTP_POST_VARS had it. The answer was the
PHP variable: $HTTP_RAW_POST_DATA. That holds content.

The receiving PHP program finds the data in the body on the HTTP POST
send,
as text your PHP can grab as: $contents = $HTTP_RAW_POST_DATA;
as text, stripped of the heading declaration, example:
<item id="ABC">
   <stuff>123456</stuff>
</item>

==================
sendXMLtoPHP.mxml
==================
<?xml version="1.0" encoding="utf-8"?>
<!--Sending an XML Object to PHP-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";>
     <mx:Script>
         <![CDATA[
             import mx.controls.Alert;
             import mx.rpc.http.HTTPService;
             import mx.rpc.events.ResultEvent;
             import mx.rpc.events.FaultEvent;

             private var xmlStr:String = '<?xml version="1.0"
encoding="utf-8" ?><item id="ABC"><stuff>123456</stuff></item>';
             private var xmlObj:XML = new XML(xmlStr);
             private var service:HTTPService ;
             private function useHttpService():void {
                 service = new HTTPService();
                 service.useProxy = false;
                 service.addEventListener("result", httpResult);
                 service.addEventListener("fault", httpFault);
                service.contentType = "application/xml";
                 service.method = "POST";
                service.url = "http://127.0.0.1/testSendXML.php";;
                 service.send(xmlObj);     // the XML object gets passed
as $HTTP_RAW_POST_DATA to PHP
             }
             private function httpResult(event:ResultEvent):void {
                 var result:Object = event.result;
                 Alert.show(result.toString(),"HTTPService Result");
             }
             private function httpFault(event:FaultEvent):void {
                 var faultstring:String = event.fault.message;
                 Alert.show(faultstring,"HTTPService Fault");
             }
         ]]>
     </mx:Script>
     <mx:TitleWindow width="250" height="200" layout="absolute"
title="Test Send XML Object to PHP">
         <mx:Button x="40" y="40" label="Send Data"
click="useHttpService();"/>
     </mx:TitleWindow>
</mx:Application>


Reply via email to