mega888 My Dev Central » Zend AMF – AS3 and PHP (part 3)

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.

First, start up Flex Builder and create a new project.

Go to File -> New -> Flex Project.

New Flex Project

Enter your project name and select your project destination. Once you’re done, click Finish.

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:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="absolute" creationComplete="init();">

        private var netConn:NetConnection = new NetConnection();
        private var resp:Responder = new Responder(onSuccess, onError);
        private var phpURL:String = ' script';

        private function init():void
            this.netConn.objectEncoding = ObjectEncoding.AMF3;
            this.netConn.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
  'Services.class1_method1', this.resp, 'Simon', 'Germain');
  'Services.class1_method2', this.resp);

        private function onNetStatus(evt:NetStatusEvent):void
            trace('Something is going on with the NetConnection object. Might be failing!');

        private function onSuccess(response:Object):void
            if (response.result == false) {
                trace('AMF Server error. Data: ' +;
            else {
                trace('Success! Data: ' +;

        private function onError(response:Object):void
            trace('AMF Client error.');

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!

Thanks for reading! Your comments are more than welcome.



  1. David says:

    Thanks for the tutorial Simon, very helpful.

  2. sgermain says:

    Glad you liked it, David. Thanks for the review! 🙂

    A lot of people underestimate the power of AMF and how easy it can be to use. PHP, being a really widespread platform, makes it very simple to implement.

    Keep reading! Your feedback is always appreciated!

  3. chris says:

    Wow, exactly what i search for a few days!

    I have test it in my own application and have the problem, that i got a bad version error. So i insert some error_log entries to see what is happened

    error_log(“init Class Services”, 0);

    class Services

    * Class 1, Method 1
    * Method 1 takes 2 arguments, $firstName and $lastName.
    * @access public
    * @param String $firstName First Name
    * @return void
    public function class1_method1()
    error_log(“class1 m1 init”, 0);
    $args = func_get_args();
    $class1 = new Class1();
    call_user_func_array(array($class1, ‘method1’, $args));

    The First error_log is triggerd, but the secound not. Have you any idea, what´s the problem?

  4. sgermain says:

    How do you call the class1_method1() method?

Leave a Reply