Simple AMFPHP Example


With the advent of Flex and RIA (Rich Internet Application) development came AMF (Action Message Format). Flex, in general, focuses only on front-end functionality, meaning it does not directly hit a database to store or retrieve persistent data. Instead, database calls are written in a more traditional back-end language like Java, PHP, Python, or .NET and such services are then exposed to be “consumed” (used) by the Flex front-end.

AMF is a protocol that allows ActionScript (the language of Flash/Flex) to call services exposed by the back-end. Additionally, if you choose, AMF allows you to translate a programmer-defined object between ActionScript and your back-end language of choice. In other words, if you have a customer class in ActionScript and you want to pass an instantiated customer object to the back-end, you simply call the exposed back-end service and pass the customer object as a parameter. The AMF layer transparently translates the ActionScript customer object into, let’s say, a customer object in Java. This functionality isn’t required. Instead of using a class you have defined, you could instead just send an integer to the backend as a parameter and, after processing, send a string back to the front-end.

Before checking out my example, I recommend checking out the Wikipedia Action Message Format article to get a basic description of AMF and links to implementations for various back-end languages. I also recommend reading AMFPHP class mapping explained by Chuck Hoffman to get a good understanding of the object translation I just described. Finally, head on over to (the creators of AMFPHP) to get a glimpse of available documentation, files, etc. I might add that if any of the folks over at don’t like me distributing AMFPHP code, let me know and I’ll modify the files I’ve posted for download. The zip file below currently contains everything you’ll need to get a simple example up and running. Now to the example…

Simple AMFPHP Example Zip File

For the example, I made a Kite Finder. Basically, you enter in a kite pilot’s name and Flex populates a person object with the pilot name entered. Flex then calls a PHP service function using AMFPHP which translates the ActionScript person object to a PHP person object on the backend. In a real environment, you would then use information from the person object to hit a database to find the pilot’s kite. In our example, we just build a kite object using dummy data. Our PHP function then returns the kite object, which AMFPHP ships back to the Flex front-end, translating it to an ActionScript kite object. In reality, the person and kite classes might be a little overboard for such a simple example. I could’ve just passed two string parameters (the pilot’s first and last name) to the PHP function and an associative array (kite info) back to the front-end, but I wanted to demonstrate AMFPHP’s object translation.

Setting up the example is fairly easy:

  1. Upload the amfphp directory to the root directory of your website.
  2. Open the SimpleAMFPHP project in FlexBuilder and modify the “gateway” variable in FindKiteCommand to point to your new gateway address.

That’s all! You should be able to deploy the project from within FlexBuilder and see it in action. This should work even if you’re using inexpensive web hosting. If you have questions on where code is, what’s happening, how to do something, or anything else, please…join the intimate conversation!

Tags: , , ,


02.28.2008 / Aaron Hardy(no joke) said:

seriously my name is aaron hardy too

Leave a Comment

Your email address is required but will not be published.