ZendAMF - hello world

Update: Lee Brimelow has post a video tutorial going over this as well, he is more detailed and its in video form, so check it out here: Lee’s video tutorial

Wade Arnold has been hard at work implementing AMF support into the Zend Framework. As someone who uses AMFPHP (another AMF implementation by Wade) regularly, I’ve been itching to get my hands on it. Tonight I was able to find a couple of hours to sit down and go over the basics of implementing it into an ActionScript project, so here is a “Hello world” example.

Get the framework

Firstly, you need to get the Zend Framework, which can be found here: Zend Framework. Right now you will need to download the Zend Framework 1.7.0 Preview Release, in order to get the AMF service, that will change in mid November when the next stable release is expected to come out. Once you’ve downloaded it, you will need to upload the library folder onto your php enabled server.

Hooking it up

The next thing is to create an PHP entry point for your Flash application to connect to. Here is a minimalist example, but all we need to get things going.

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../library');

require_once 'Zend/AMF/Server.php';
require_once 'HelloWorld.php';

$server = new Zend_Amf_Server();
$server->setClass('HelloWorld');

echo($server->handle());

Now with this file on the server, we can connect to it from Flash, but first we must create the HelloWorld php class. This class will actually contain any of the function that we want to call from the Flash player. For this example our class will simple return a string. Here is what the HelloWorld class looks like:

class HelloWorld {
    public function sayHello() {
        return 'Hello Zend AMF, thanks Wade!';
    }
}

That’s it on the Zend/PHP end of things, next we need to setup a simple ActionScript application to connect to it using a NetConnection Object.

//create the Net Connection object
var connection:NetConnection = new NetConnection();
connection.connect("http://localhost:8888/site/AMF_Entry.php");
connection.call('HelloWorld.sayHello', new Responder(displayResponse));

function displayResponse(message:Object):void {
    trace(message);
}

And that’s it! Hopefully that will help you get started working with ZendAMF.

Comments

Comment functionality has been disabled. Contact me on Twitter.

Matthew Weier O'Phinney said:

To make your service classes re-usable with other server protocols in Zend Framework (XML-RPC, JSON-RPC, SOAP), you should provide docblocks for all class methods that minimally provide the parameter types and return type. If you do, you can use the exact same code you’ve presented for Zend_Server_Amf with each of them. :)

leef said:

Do you have a preference for either PHP AMF tool, AMFPHP, or ZendAMF? I’m still limited to AMFPHP due to Zend’s PHP5 requirement, and a server admin who cannot seem to justify upgrading our production server. /cue violin

Tyler Egeto said:

PHP 4? Ewww… Of course all PHP is fairly ewww in my opinion. Anyway, they are both great solutions! I’m using AMFPHP in several projects at work right now, and the Zend extension on some private stuff. AMFPHP is great for its ease of use and fantastic service browser, which can be really helpful when debugging.

Here is a link to Wade’s thought on the subject. (The brain behind them both)

http://wadearnold.com/blog/?p=112

Aryan Duntley said:

Ok. Thank you. However, this tutorial/explanation is very similar to all the others out there in that it ONLY explains how to receive data from PHP to AS3. How, PLEASE, would I send data FROM AS3 to PHP using ZendAMF???

Recent & Popular Articles

Browse All >