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