Systeme D

4 January 2007

Lightweight AMF (Flash remoting) for PHP

AMF, the ActionScript Message Format, is a neat way of communicating between an SWF in the user's browser and a server-side script. (Its most well-known use is for Flash Remoting and the two names are sometimes used synonymously.)

In brief, the SWF can send a remote procedure call over HTTP. Once the server has carried out the task, it sends a response back, which is picked up asynchronously by a responder function in the SWF. Potlatch uses this to read and write to a serverside map database.

AMF has been successfully reverse-engineered (docs here) and several open-source implementations exist. Just three: AMFPHP, SabreAMF, WebORB. All of these implement the AMF protocol wonderfully completely and are great for Real Programmers.

Unfortunately I'm not a Real Programmer and have barely advanced past the days of the CPC firmware, so having to install any package with more than two files makes my head hurt. Anyway, for comparatively modest projects, it can be good to eliminate complex dependencies and just drop a single file in.

So I went back to the very first open source implementation of AMF, a single-file PHP class developed by Wolfgang 'Musicman' Hamann (who is one of the geniuses behind Ming). This does almost everything you need for simple AMF apps in 200 lines of PHP. (Background here, more here.)

It required a little modification to work with modern Flash players:

  • Flash Player will batch several AMF requests into one HTTP call.
  • AMF has a 'MixedArray' data type very much like its Object data type.
  • Empty arrays seemed to cause a problem.

So I've fixed these three, tidied the code a bit, and here it is:

amfdata.php

Hope someone finds it useful. There are outline docs in the comments, but let me know if you have any more detailed questions.


Comments


Add a comment

Your name:

E-mail address: 

Comment:

your comment. (E-mail addresses will not be visible, but a server-based mail link will be provided. To guard against spam, no comments which include 'http' or 'www' will be published.)