9 Replies Latest reply on Oct 11, 2009 6:53 AM by flex_n00b

    How to use AMFPHP Objects in Flex

    flex_n00b

      Hi,

       

      I have been successful in getting amfphp to return PHP objects, the question now is how can I make Flex aware of the objects?

       

      I am invoking a remote object and the objects are returned as a result. Can they be used directly or should I use XML?

        • 1. Re: How to use AMFPHP Objects in Flex
          Francisc Level 3

          Hey!

          Yes, you can use them directly by assigning the returned object to an ArrayCollection Class variable.

           

          If you need further helo, I can provide some code to show you what I mean.

          Also, if you like, paste some of your code here to see where the problem is.

           

          Good luck!

          1 person found this helpful
          • 2. Re: How to use AMFPHP Objects in Flex
            flex_n00b Level 1

            Hi,

             

            Thanks for your reply, it will be really useful if you can post some code.

             

            As I told you, the code I have is-

             

            For the Remote Object-

             

            <mx:RemoteObject id="rmObj"   
                    endpoint="http://localhost/amfphpsamples/gateway.php"
                    destination="amfphp"
                    showBusyCursor="true"
                    source="com.vb.FirstScript"/>
            

             

            And the response handler

             

            private function handleResponse(event:ResultEvent):void
                        {
                            rmObj.removeEventListener(ResultEvent.RESULT,
                            handleResponse);
                            rmObj.removeEventListener(FaultEvent.FAULT,
                            handleFault);
                            //do work here to get results, how do i do that....
                                          
                        }
            
            • 3. Re: How to use AMFPHP Objects in Flex
              Francisc Level 3

              Hmm, your code seems a bit to complicated. Here's what I do:

               

              The RemoteObject:

               

              <mx:RemoteObject id="PHPGateway" source="yourAMFPHPClassName.ThePHPFile" destination="amfphp" showBusyCursor="true">

                  <mx:method name="theFunctionYouWantToCall" result="myFunctionResultHandler(event)" fault="myFunctionFaultHandler(event)"/>

              </mx:RemoteObject>

               

              Observations:

              1. yourAMFPHPClassName is the name of the folder in amfphp\services that contains the PHP classes.

              2. ThePHPFile is the PHP class file (without the extension) in the yourAMFPHPClassName folder.

              3. You need the <mx:method name="..." .../> to define the function in the PHP class you want  to call. The theFunctionYouWantToCall must be the exact name of the function in the PHP class (case sensitive).

               

               

               

              The ActionScript part:

               

              //THE BELOW EXAMPLE ONLY WORKS IF THE PHP FUNCTION RETURNS A SINGLE VALUE

              //This means the PHP function has a return statement like this: return $value; or return "Test";

              private function myFunctionResultHandler(event:ResultEvent):void

              {

              var PHPResult:String=String(event.result);

              Alert.show(PHPResult);//will show what the PHP class returned

              }

               

               

              //THE BELOW EXAMPLE SHOWS HOW TO PASS MULTIPLE DATA TO FLEX

              //This means the PHP function has a return statement like this:

              //$returnValue=array();

              //$returnValue['name']="George";

              //$returnValue['age']=$age;

              //return $returnValue;

              private function myFunctionResultHandler(event:ResultEvent):void

              {

              Alert.show("Name: "+event.result.name);

              Alert.show("Age: "+event.result.age);

              }

               

              //THE FAULT HANDLER

              private function myFunctionFaultHandler(event:FaultEvent):void

              {

              Alert.show("Data could not be acquired.");

              }

              • 4. Re: How to use AMFPHP Objects in Flex
                flex_n00b Level 1

                Thanks a lot for your wonderful post, it was really helpful.

                 

                You rock!

                • 5. Re: How to use AMFPHP Objects in Flex
                  Francisc Level 3

                  You're welcome!

                   

                  I forgot to mention that you can use as many functions as you like from the AMFPHP class knowing that for each you need to write the method MXML tag and assign result and prefferably fault handlers:

                   

                  <mx:RemoteObject ...>

                     <mx:method name="getPerson" result="getPersonResultHandler(event)" fault="getPersonFaultHandler(event)"/>

                     <mx:method name="addPerson" result="addPersonResultHandler(event)" fault="addPersonFaultHandler(event)"/>

                     <mx:method name="updatePerson" result="updatePersonResultHandler(event)" fault="updatePersonFaultHandler(event)"/>

                     <mx:method name="deletePerson" result="deletePersonResultHandler(event)" fault="deletePersonFaultHandler(event)"/>

                  </mx:RemoteObject>

                  1 person found this helpful
                  • 6. Re: How to use AMFPHP Objects in Flex
                    flex_n00b Level 1

                    Also, how can I return a PHP object that ActionScript can identify using amfphp?

                     

                    I read that this is possible, and incorporated in Gumbo, but don't know how to do it in Flex3

                    • 7. Re: How to use AMFPHP Objects in Flex
                      Francisc Level 3

                      The most complex structure I've sent from PHP via AMFPHP to Flex 3 is the array.

                      I am not sure if it will work, but you can try sending the PHP object like return $object; and use the debugger from Flex 3 to see what data you get and how it is structured.

                       

                      But again, I am not sure if it works.

                      • 8. Re: How to use AMFPHP Objects in Flex
                        flex_n00b Level 1

                        Hey, one last thing (really sorry to annoy you so much), I tried to retrieve an array from PHP, which worked fine (I checked it in the amfphp services browser, and its returning an array).

                         

                        But when I try to use that in Flex, it gives a cast error. What is the correct way to do it?

                         

                        I am retrieving from the database and thus array elements have indeces, and not names - which Flex does not seem to like and throws an error.

                        • 9. Re: How to use AMFPHP Objects in Flex
                          Francisc Level 3

                          Well, it depends how you get the data.

                          If you use mysql_fetch_array($resource,MYSQL_ASSOC) it will return data as Array with the column names instead of their numbers.

                           

                          What I usually do is this:

                          $sql="select ...";

                          $resource=mysql_query($sql);

                          $returnArray=@mysql_fetch_array($resource,MYSQL_ASSOC);

                          return $returnArray;

                           

                          You can use the Flex app provided with AMFPHP in the browser folder to view data sent to Flex.