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>:<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


September 4, 2008 at 3:12 pm
[...] http://sujitreddyg.wordpress.com/2008/09/04/invoking-php-functions-from-flex-using-amfphp/ [...]
December 9, 2008 at 10:39 pm
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.
December 11, 2008 at 2:37 pm
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
February 19, 2009 at 8:34 am
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.
February 20, 2009 at 1:53 pm
Hi Param,
I wouldn’t recommend this approach. Try sending a HTTP request to your PHP from Java class.
Hope this helps.
February 21, 2009 at 5:24 pm
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 ?
February 21, 2009 at 5:25 pm
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 ?
February 21, 2009 at 5:33 pm
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
February 24, 2009 at 7:57 pm
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.
February 24, 2009 at 8:29 pm
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 ?
March 9, 2009 at 1:23 pm
Hi Sujit,
Good Article
March 9, 2009 at 3:10 pm
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.
March 9, 2009 at 6:08 pm
Thanks Abbas
April 5, 2009 at 10:31 pm
Hi thanks, this yhe simpliest i’ve seen
)
(i’ve adde cross-domai-policy)
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
Any idea???
April 9, 2009 at 9:12 pm
Hi David,
When you get the FaultEvent, please check the faultDetail and rootCause properties for more details on the error.
Hope this helps.
July 27, 2009 at 1:24 pm
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.
August 28, 2009 at 6:17 pm
Hi Binod,
Thanks for reporting this
Please try this http://sujitreddy.g.googlepages.com/FirstService.php.txt
September 14, 2009 at 7:45 pm
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?
October 9, 2009 at 2:03 pm
Hi Sujit,
In the example, what is the use of the destination attribute for the Remote Object?
November 5, 2009 at 9:29 pm
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”