Development best practices and research

Zend AMF – AS3 and PHP (part 3)

In this part, I will explain the Flex side of the connection.

There are multiple ways of connecting to a back end with Flex. There’s the “Java” way, with the XML files and there’s the “Flash” way, using a NetConnection object. This part will be covering the NetConnection way.

You should be presented with a simple main.mxml file, containing your mx:Application tag. We will need to add a listener to the event CREATION_COMPLETE in order to start our communication with PHP. Here’s main.mxml’s code:

So, the code executes some very simple tasks. It creates the NetConnection object, gives it the right object encoding for Zend AMF Server and connects to the PHP back end. Once the connection is done, it calls the first two methods in our Services proxy class. Zend AMF Server will call its registered class, called Services, and invoke the method “class1_method1”, passing it two arguments, first name and last name. It then calls a second method called “class1_method2”. Note that both calls use the same responder. They don’t have to. For the same of that example, I make the call the same responders, since I want to do the same thing with both calls.

You can now go ahead and run your application in Debug mode. Otherwise, you won’t see your traces. Go to the Run menu and select Debug.

There you are, if you followed this example, you should have a very simple connection between Flex and PHP. Of course, you have full access to both technologies. Let your imagination run wild!