Skip to content

AMF Flash Remoting with Zend and CI

World Wide Web Server edited this page Jul 4, 2012 · 15 revisions

[h3]This is a tutorial on how to implement Flash Remoting in CodeIgniter using the Zend Framework.[/h3]

[h3]Description:[/h3] Action Message Format(AMF) is a binary file format representing a serialized ActionScript object. The AMF file type is used throughout the Flash Player for data storage and data exchange. For example in the Flash Player AMF is used in SharedObjects, RemoteObjects, LocalConnection, ByteArray, RTMP, and all RPC operations. Some of the benefits of AMF include:

[b]*File Size[/b] - AMF objects are very small and are compressed using zlib. [b]*Fast Serialization/ Deserialization[/b] - AMF is transformed using native C code in the Flash Player making it very fast. The AMF format was designed to serialize and deserialize quickly under low memory and slower CPU conditions making it perfect for the web. AMF data is parsed directly into objects, meaning there is no lag for interpretation or parsing of AMF making the creation of objects complete in a single pass. [b]*Native Types and Custom classes supported[/b] - You can serialize any object in Flash Player with the only exception being a displayObject.you can also map serialized objects back to custom class instanced provided the custom class is in the Flash Player when the AMF object is deserialized.

AMF existed in ActionScript 2 and was just called AMF as of ActionScript 3 the AMF protocol has been updated and is referred to as AMF3. For historical reasons the original AMF format is now referred to as AMF0. One of the main upgrades to AMF3 is that the object is now zlib compressed for faster transfer do to the smaller file size and the additional of data types that were released with ActionScript 3.

[h3]Author:[/h3] Matt Johnson Lead Interactive Developer Coolfire Media St. Louis, MO USA E: [url=mailto:[email protected]][email protected][/url] W: [url=http://www.coolfiremedia.com]www.coolfiremedia.com[/url]

[h3]The following components are needed to make this work:[/h3] 1: CodeIgniter Installation 2: The Zend Framework (minimal) 3: Flash CS3 (Actionscript 3)

[h3]Part 1: Download and integrate the Zend Framework[/h3] b[/b] Start by visiting [url=http://framework.zend.com]framework.zend.com[/url] and download the current minimal package, which at the time of authoring is 1.7.2. This will provide you with all the core components needed to implement AMF into your project. Once unzipped you should create the following folder and copy over your Zend framework files. Make sure the Zend package contains the AMF directory.

[code]system->application->libraries->Zend[/code]

b[/b] Now update your config file to enable hooks if this has not been done already.

[code] (system->application->config)

$config['enable_hooks'] = TRUE; [/code]

b[/b] Define a generic hook pre-controller for using Zend and other frameworks

[code] (system->application->config->hooks)

$hook['pre_controller'][] = array( 'class' => 'App_hook', 'function' => 'index', 'filename' => 'app_hook.php', 'filepath' => 'hooks' ); [/code]

b[/b] Create the app_hook described in the pre-controller to make sure your Zend files are located and loaded correctly in the include path.

[code] <?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class App_hook { function index() { ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/libraries/'); } } ?> [/code]

b[/b] Finally, create a library file that you can use to load Zend classes or other framework classes that you may install later. There are a lot of different options here, but I typically like to load an array through the constructor or individual items through the load method shown below. Use whatever method suits you best.

[code] <?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}

class Apps { function __construct($class = NULL) { if(!empty($class) && is_array($class)) { foreach($class as $item) { $this->load($item); } } }

function load($class)
{
    require_once (string) $class . EXT;
}

}

?> [/code]

[h3]Part 2: Load Zend AMF and create a Remoting Class[/h3]

b[/b] In your application directory, create a new controller called "remote.php" and implement Zend AMF by loading the server class and creating a new AMF Server object. In the setClass method, use "$this" so the server knows to find the requested method in the Remote controller itself.

Then call the handle() method which will process the incoming AMF request and return the data to your Flash application. At this point you should also be able to point your browser to this controller and receive the message "Zend AMF Endpoint" which lets you know that everything is running as expected.

[code] class Remote extends Controller { function Remote() { parent::Controller();
}

function index()
{
    $classes = array("Zend/Amf/Server");
    $this->load->library("Apps",$classes);
    
    $server = new Zend_Amf_Server();
    $server->setClass($this);
    echo $server->handle();
}

function getData()
{
    $data = array(1,2,3,4,5);
    return $data;
}

} [/code]

[h3]Part 3: Create a new Flash CS3 application and test the connection[/h3] b[/b] Open Flash CS3 and create a new ActionScript 3 Flash File. Then open the actions panel and enter the following code.

[code] import flash.net.*;

var nc:NetConnection = new NetConnection(); nc.connect("http://www.testsite.com/remote");

var resp:Responder = new Responder(onResult,onError);

function onResult(e:Object) { trace(e); }

function onError(e:Object) { trace(e); }

nc.call("Remote.getData",resp); [/code]

This code will create a new NetConnection object and attempt to make a connection to the remote controller that we created earlier. In this case I have it running on a local test server so your address will be different. Create a new Responder object and give it the names of your methods for handling successful requests and errors, then simply define those two Responder methods and trace out the results to test your Zend AMF Server.

Finally, call the Zend AMF Server through the NetConnection and access the simple getData method. If everything is working correctly, Flash will output "1,2,3,4,5" letting you know that the array of integers was transferred from PHP to Flash.

[h3]Closing Remarks:[/h3] This is just a simple example of using AMF and you can get much more complex. A great resource for learning more is from the original AMFPHP project or through the Community Wiki at Zend url=http://framework.zend.com/wiki/display/ZFPROP/Zend_Amf+-+Wade+Arnold[/url]

The Zend programming documentation for AMF can be found here [url=http://framework.zend.com/manual/en/zend.amf.html]http://framework.zend.com/manual/en/zend.amf.html[/url]

Clone this wiki locally