4 Replies Latest reply on Oct 20, 2008 9:12 AM by KevPartner

    How to retrieve a string value from a PHP function call

    glen08
      Should this string be the return value of the PHP function? Or should it be embedded in the response HTML? Any examples?
        • 1. How to retrieve a string value from a PHP function call
          glen08 Level 1
          This is what I got so far:

          footer.mxml in Flex Builder 3:
          ------------------------------------------
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="ts_request()">
          <mx:Script>
          <![CDATA[
          public function ts_request():void {
          timestampRequest.send();
          }
          ]]>
          </mx:Script>
          <mx:Canvas width="836" height="36" backgroundColor="#05075C">
          <mx:Label id="timestamp" text="{timestampRequest.lastResult.timestamp}" color="#FEFEFE" height="20" y="10" textAlign="left" width="221" x="10"/>
          </mx:Canvas>
          <mx:HTTPService id="timestampRequest" url=" http://www.ABC.com/timestamp.php" useProxy="false" method="POST" />
          </mx:Application>

          Here is the timestamp.php on the www.ABC.com web server. I have tested the php by directly access it with "www.ABC.com/timestamp.php" and it shows the timestamp correctly:
          ----------------------------------------------------------------------------------------
          <?php
          putenv($timezone);
          $Return = "<timestamp>".date("m/d/Y H:i:s", time())." CST"."</timestamp>";
          print($Return);
          ?>

          When I run footer.mxml in Flex 3 Builder, I got an error: This file cannot be launched.

          Where did I do wrong?

          • 2. Re: How to retrieve a string value from a PHP function call
            chris.huston.t10 Level 3
            Debugging HTTPService calls is a lot easier if you listen for result and fault events rather than using lastResult. When I tried to access the timestamp.php url directly, I could not. Here is the code for using result events:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="ts_request()">
            <mx:Script>
            <![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            public function ts_request():void {
            timestampRequest.send();
            }

            private function abc_result(evt:ResultEvent):void {
            trace(evt.result);
            }

            private function http_fault(evt:FaultEvent):void {
            trace(evt.fault);
            trace(evt.message);
            }

            ]]>
            </mx:Script>
            <mx:HTTPService fault="http_fault(event)" result="abc_result(event)" id="timestampRequest" url=" http://www.ABC.com/timestamp.php" useProxy="false" method="POST" />
            </mx:Application>
            • 3. Re: How to retrieve a string value from a PHP function call
              SujitG Level 2
              Hi,

              There are couple of ways in which you can retrieve a String from a function
              call.
              First is to use HTTPService: In this you will output the String you want to return in the output and then parse it in the Flex application. This is very straight forward and you can find sample at the URL below.

              http://livedocs.adobe.com/flex/3/html/data_access_2.html#196471

              Second is to use Remoting: In this method you will use AMFPHP. AMFPHP will invoke the PHP function for you and will return the String as a String to your Flex application. In you Flex application you can access the String as an String object and use it. Please find sample at the URL below.

              http://sujitreddyg.wordpress.com/2008/09/04/invoking-php-functions-from-flex-using-amfphp/

              Hope this helps.
              • 4. Re: How to retrieve a string value from a PHP function call
                KevPartner Level 1
                Just to give a different perspective, I've been doing a LOT of this lately and by far the most effective approach I've found is to use AS3 code rather than mixing with MXML. This allows for much simpler debugging, in my experience.

                //THIS FUNCTION SETS IT UP
                private function fetchdata():void
                {
                //set up the request
                var request:URLRequest=new URLRequest(" http://www.ABC.com/timestamp.php");
                request.method=URLRequestMethod.POST;

                //activate it
                var loader:URLLoader=new URLLoader(request);
                loader.addEventListener(Event.COMPLETE, fetchdatacomplete);
                }

                //THIS FUNCTION IS CALLED WHEN THE DATA COMES IN
                private function fetchdatacomplete(event:Event):void
                {
                var receiveddata:XML=new XML(event.target.data);
                trace(receiveddata);
                }


                This will trace out the entire data string sent by PHP so you can diagnose whether the data contains the variable you're looking for. If this event handler is never called then there's a problem with communication. In that case, add a new EventListener like this:

                loader.addEventListener(Event.httpStatus,fetchdataprogress);

                immediately after the other eventlistener. Then, trace out the status in a new function:

                trace(event.status);

                which will give you the HTTP status code at various stages of the transfer.

                Hope that helps.