Invoking PHP functions from Flex using AMFPHP

You can invoke functions in a PHP class from a Flex application using Remoting as we used to do with Java classes. I used AMFPHP (http://www.amfphp.org/ ) for invoking PHP functions.

In this article we will see how to set up AMFPHP on the system and then we will create a Flex application to invoke the PHP function and also create PHP function which will serve our request.

Note: You need to have a web server with PHP 5 configured to have this sample working.

Setting up AMFPHP

1.       Download amfphp-1.9.beta.20080120.zip under amfphp_1.9_beta2 from this URL http://sourceforge.net/project/showfiles.php?group_id=72483#files

2.       Create folder called amfphpsamples in the document folder of your Apache server configured with PHP 5

3.       Extract the amfphp-1.9.beta.20080120.zip file to amfphp-1.9.beta.20080120

4.       From extracted amfphp-1.9.beta.20080120\amfphp folder, copy services folder, core folder, gateway.php and globals.php into the amfphpsamples folder created above

5.       Now we have our AMFPHP set up in our web application folder

6.       Try invoking gateway.php and see if it is set up properly. (http://<servername&gt;:<portnumber>/amfphpsamples/gateway.php)

Creating PHP class

1.       Create file named FirstService.php under amfphpsamples/services/com/adobe

2.       Open the FirstService.php and paste the code from the PHP class at this URL http://sujitreddy.g.googlepages.com/FirstService.php.txt

3.       In our PHP file, we have a class called FirstService

4.       In the class we have a function called greetUser()

5.       Now we have our PHP class ready

Creating Flex application to invoke PHP function

1.       Create Flex project using your Flex Builder

2.       Copy the code from this file into your application MXML file http://sujitreddy.g.googlepages.com/MyPhpSamples.mxml

3.       In the code you can see a RemoteObject with endpoint property pointing to the gateway.php

4.       RemoteObject also has a source property set to com.adobe.FirstService, which is the class we created above

5.       On button click we are adding event listeners to ResultEvent.RESULT and FaultEvent.FAULT events of the RemoteObject

6.       If the response is successful the result event handler is invoked and fault handler is invoked if there is a problem invoking the service

7.       In the result handler you can notice that we are using event.result to retrieve the response from the server.

8.       event.result will contain the response from the server

You can try the sample at this URL http://www.sujitreddyg.com/amfphpsamples/flex/FirstRemotingSample.html

That’s it we have our first Remoting sample using Flex and PHP working. I will post more samples using WebORB PHP soon :)

About these ads

22 Responses to Invoking PHP functions from Flex using AMFPHP

  1. Steven Rieger says:

    Hi Sujit,

    I am curious how this sample is using amfphp to do anything? It appears it’s just like calling the .php file in a browser. Or am I missing something? I’m very new to php and interested in using it to gather data from a MySQL db

    Any thoughts you might have on this would be appreciated.

  2. Sujit Reddy G says:

    Hi Steven,

    Yes, it is just a simple call to a PHP file but that PHP file is part of amfphp. This PHP file take care of handling the AMF data passed from Flex application and then invoke appropriate function in a class. If you can see we are passing AS objects to PHP file, which are converted to PHP objects by this PHP file. This PHP file acts like a gateway to amhphp code.

    Hope this helps :)

  3. Param says:

    Hi Sujeet,
    We are using BlazeDS for our Flex application. But our authentication piece is done in PHP. And also there are couple of PHP Application uses those functionality using AMFPHP. I was wondering how i can re-use those functionality that is written in PHP. Can i call PHP from my Java remote objects, which is called by my BlazeDS.

  4. Hi Param,

    I wouldn’t recommend this approach. Try sending a HTTP request to your PHP from Java class.

    Hope this helps.

  5. Noel says:

    Hi Sujit,

    I want my php code to accept a username and password and return xml of the form

    yes

    How can i access just the “yes” part from the xml result ?

  6. Noel says:

    Hi Sujit,

    I want my php code to accept a username and password and return xml of the form

    “yes”

    How can i access just the “yes” part from the xml result ?

  7. Noel says:

    Hi Sujit,

    I want my php code to accept a username and password and return xml of the form

    xmltag: loginsuccess yes /loginsuccess

    How can i access just the “yes” part from the xml result ?

    Sorry about the double posts…couldnt delete them

  8. Hi Noel,

    Use HTTPService to send a POST request to your PHP with username and password, set the resultFormat of the HTTPService to object. You can access the data returned in XML format as objects. You might also want to try e4x as a resultFormat for HTTPService. Please find more details at the URL below.

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

    Hope this helps.

  9. Noel says:

    hi Sujit,
    thanx for the info…would it be better to use HTTPService or Remoting using amfphp for accessing a mysql database…if Remoting is better could u provide some links if any which have information on retrieving several rows from a mysql database and displaying them in a datagrid…also when using Remoting if i return an array of rows from a database to an Object…how can i access a particular row’s values from the Object ?

  10. Abbas Ali Butt says:

    Hi Sujit,
    Good Article

  11. Hi Noel,

    I would use Remoting, because its efficient and it lets me deal with objects I passed from PHP rather than depend on some intermediate format like XML or JSON.
    Just create object for each row in the database and add it to a array. If you invoke a PHP function using AMF PHP or Zend AMF which returns an array containing objects, in Flex application you will receive it as ArrayCollection containing the objects.

    Hope this helps.

  12. david says:

    Hi thanks, this yhe simpliest i’ve seen :-))
    BUT, it works in localhost but it don’t if I upload it to the server (it says “send failed”) The service works when runned from the gateway.php, but fails when called from swf :-? (i’ve adde cross-domai-policy)
    Any idea???

  13. Hi David,

    When you get the FaultEvent, please check the faultDetail and rootCause properties for more details on the error.

    Hope this helps.

  14. Binod says:

    Hi Sujit
    When I open http://www.sujitreddyg.com/googlepages/FirstService.php it comes blank can you please let me know what actually its written there . I just wrote a class and a method like

    but I am getting error as channel disconnected. So please help me.

  15. Hi Binod,

    Thanks for reporting this :) Please try this http://sujitreddy.g.googlepages.com/FirstService.php.txt

  16. Himanshu says:

    Hi Sujit

    I have been successfully using remoting with PHP at server and Flex as client.

    My requirement is to poll my PHP service after regular intervals. So is it a good idea to invoke the service using RemoteObject in a timer function? it will return an arraycollection and every time my my data provider will change so item renderer objects will recreate, which causes flickering in front end.
    So do we have some better alternative where we only get the changed/ updated results from the server?

    What is the proper technique to solve this problem?

    Does BlazeDS with polling enabled help in such cases?

  17. Shreyas says:

    Hi Sujit,

    In the example, what is the use of the destination attribute for the Remote Object?

  18. wilfredo says:

    Hi,

    I am getting this error. If I run the example. I have Tomcat 6.0, blazeds, PHP 5.26 and AMFPHP 1.9…
    I can run the gateway find but not the browser example or your example…

    Please help

    (mx.rpc::Fault)#0
    errorID = 0
    faultCode = “Client.Error.MessageSend”
    faultDetail = “Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed”
    faultString = “Send failed”
    message = “faultCode:Client.Error.MessageSend faultString:’Send failed’ faultDetail:’Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed'”
    name = “Error”
    rootCause = (Object)#1
    code = “NetConnection.Call.Failed”
    description = “HTTP: Failed”
    details = “http://localhost:8080/amfphp/gateway.php”
    level = “error”

  19. V says:

    Thanks for this tutorial!!

  20. Ravi Mone says:

    Sir,

    Can u please explain how to maintain session management
    with flex and php application, or any url will help me a
    lot,

    thanks and regards
    Ravi.M

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 111 other followers

%d bloggers like this: