Saturday, March 30, 2013

AMFPHP with the Flash Vector class

The AMFPHP (1.9 beta) did not support vectors (vector arrays) either inbound or outbound from Flash. My colleagues and I have made this work for our team, but with PHP 5.4 and a rather modified version of AMFPHP for our sole purposes.

There is a new version of AMFPHP from Silex Labs. I cannot say how much better it is than the 1.9 version, but I would advise that you check that first. I'm not sure though that it includes code for serializing and deserializing vectors, and for that reason, I have posted the code from our adaptation of the 1.9 version so that its potentially available to anybody, new version or old.

I have created a google code project and included the updates to AMFSerializer and AMFDeserializer there. These are the the .php files, not patches, so you will need to know what you're doing to update your code. If there really is enough interest, I will create patches and tidy this up a bit. There is also a class for AMFVector, this is so that the serializer knows that we want to send a vector back (and not an array). It also defines constants for the serializer and deserializer.

Vectors are cast to arrays for incoming vectors to PHP. Arrays wrapped in the AMFVector class will be vectors when they get back to flash. All the vectors in the spec cast back correctly, but please note the Vector.<Boolean> and Vector.<String> get cast to Vector.<Object> by Flash on the swing-back for some reason. I found that casting them using the static Vector.<String> or Vector.<Boolean> function call addressed the issue (ie: without the "new"). 

Please be warned, this was made for PHP 5.4 - it is not backwards compatible with other versions of PHP, and there is no intention on my part to make it so. Also, there are no guarantees, you use this at your own risk.

At the same time, as far as I can see this conforms the AMF spec for vectors, and works for vector-int, vector-uint (with the PHP caveats), vector-float and vector-object.