- Notifications
You must be signed in to change notification settings - Fork4
AMF serialization/deserialization for PHP
License
infomaniac-amf/php
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
AMF (Action Message Format) is a binary data serialization protocol. Simply put, it transforms objects in memory into a binary string, and can reverse this binary string into an object in memory. It can be used just likeJSON
, and this library has been build to provide a similar API to that exposed by theJSON
functionality inPHP
.
The purpose of this library is to provide a consistent and symmetric implementation of theAMF
specification in bothPHP
&JSON
.
Well, it's up to you.JSON
is perfectly suited to the web, however it does have some shortcomings which are addressed byAMF
. For starters,JSON
cannot handle complex object graphs with circular references; additionally, it cannot serialize dates & byte arrays - you would need to do some additional work to support these inJSON
(convert date to unix timestamp, byte arrays to base64).
Hells no.JSON
is great;AMF
can simply provide you with some additional funcitonality which could help you build your web app.
To begin using this library, you will need to install it viaComposer:
{"require": {"infomaniac-amf/php":"dev-master"}}
Once you have runcomposer install
, you will be able to use the library by simply includingComposer
's autoloader:
<?phprequire_once'vendor/autoload.php';
Here is a simple example of encoding an array toAMF
:
<?phprequire_once'vendor/autoload.php';$data =array('any' =>'data','you' =>'like');header('Content-Type: application/x-amf');echoamf_encode($data);
This will produce a binary string which represents your given data.
If you were to inspect the HTTP traffic of a page that producesAMF
data, using a tool such asCharles Proxy, you would see the following output:
To decode this string, simply do the following:
$data =array('any' =>'data','you' =>'like');$encodedData =amf_encode($data);$data =amf_decode($encodedData);
If you were tovar_dump
this data, it would look identical to the input data given to theamf_encode
function.
array (size=2) 'any' => string 'data' (length=4) 'you' => string 'like' (length=4)
AMF
allows you to encode an object and retain some metadata about it; for example, when serializing an instance of a class (notstdClass
) the library will retain the class' fully qualified namespace name and use it to reconstruct an object of that type upon decoding.
Consider the following class:
<?phpclass Person {public$name;}
If we encode an instance of this object, by default its class type will be ignored and when the data is decoded, the resulting value will be a plain PHPstdClass
instance.
This is how the encoded data will look in Charles:
In order to retain the class type inAMF
, you will need to add an additional flag to theamf_encode
function call:
amf_encode($data,AMF_CLASS_MAPPING);
When theAMF_CLASS_MAPPING
flag is given, the encoded data will look like this in Charles:
Notice the additionalPerson
metadata in this response
Now, when this data is decoded, the library will attempt to create a new instance of thePerson
class and set its public propertyname
to"bob"
.
object(Person)[8] public'name' => string'bob' (length=3)
TheAMF
spec allows for the serialization of several different data-types.
Here is a link to the latest specification:AMF3 Spec - January 2013
This library implements10 of the18 data-types described in the specification. The reason for the support of only asubset of these types can be seen in two lights:utility andlimitation. Here is an exhaustive list of the data-types available:
Data-Type | Included | Reason for exclusion |
---|---|---|
Undefined | ✔ | - |
Null | ✔ | - |
False | ✔ | - |
True | ✔ | - |
Integer | ✔ | - |
Double | ✔ | - |
String | ✔ | - |
XML Document | ✗ | Who needs XML? |
Date | ✔ | - |
Array | ✔ | - |
Object | ✔ | - |
XML | ✗ | Who needs XML? |
ByteArray | ✔ | - |
Vector | ✗ | Not high priority - also, possible browser incompat issue with JS |
Vector | ✗ | Not high priority - also, possible browser incompat issue with JS |
Vector | ✗ | Not high priority - also, possible browser incompat issue with JS |
Vector | ✗ | Not high priority - also, possible browser incompat issue with JS |
Dictionary | ✗ | PHP cannot use objects are array keys |
This project is licensed under theMIT
license.
While writing this library, I used several libraries to validate my progress, and to help me come unstuck. I would like to extend a special thanks to the following libraries and individuals:
- SabreAMF
- AmfPhp
- Charles Proxy's wonderful AMF implementation
- Arseny Vakhrushev (neoxic) for his patience, guidance, advice and help
- Robert Cesaric,Grant McMullin andAndre Venter for their insight and advice
- My esteemed colleagues atZando
About
AMF serialization/deserialization for PHP
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.