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

Posted by Got my Domain « Sujit Reddy G - The Evangelist on September 4, 2008 at 3:12 pm
[...] http://sujitreddyg.wordpress.com/2008/09/04/invoking-php-functions-from-flex-using-amfphp/ [...]
Posted by Steven Rieger on 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.
Posted by Sujit Reddy G on 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
Posted by Param on 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.
Posted by Sujit Reddy G on 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.
Posted by Noel on 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 ?
Posted by Noel on 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 ?
Posted by Noel on 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
Posted by Sujit Reddy G on 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.
Posted by Noel on 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 ?
Posted by Abbas Ali Butt on March 9, 2009 at 1:23 pm
Hi Sujit,
Good Article
Posted by Sujit Reddy G on 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.
Posted by Sujit Reddy G on March 9, 2009 at 6:08 pm
Thanks Abbas
Posted by david on 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???
Posted by Sujit Reddy G on 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.