Application Development at AMFPHP

Oddly enough, on the vast Internet, I did not find a single full-fledged manual (in any case, Russian) for developing applications on AMFPHP. About a year ago, I really needed this kind of article, but for lack of it I had to collect everything grains from different sources. I hope she will be useful to someone.


So what is AMF? We will not reinvent the word bike and turn to the wiki for help:

AMF (Action Message Format) is a binary data exchange format used in applications written in Action Script. It is built on the basis of the Simple Object Access Protocol and is used primarily for the exchange of information between Adobe Flash and databases. Action Message Format is more economical in traffic than XML and allows the transfer of typed objects. It was announced on December 13, 2007.

At the moment, AMF is the most relevant data transfer format between a flash application and the server part. Libraries for working with AMF exist for almost all server languages, but today I would like to dwell on PHP.

AMFPHP vs. Zend amf

After you have chosen PHP as the server part of your application, you should decide which package to work with this format you will use in the future. At the moment, there are two of the most relevant projects: AMFPHP and Zend AMF. There is not much difference between the two products, so it is rather a matter of taste, but I chose AMFPHP for my projects because:
  • It is lighter and more compact.
  • It is an independent project, and not part of any framework.
  • According to test results, AMFPHP is at least 3 times faster than its competitor
  • I do not like Zend as such

Beginning of work

Since this is a manual on AMPHP, not AMF in general, I will focus on the server part directly. In addition, the flash, for the most part, does not matter what kind of beast is on the server - all requests are universal for AMF format, and hence a large number of relevant materials on the network.

Suppose the root of our project is the root of the web server. In my manual I will designate it as / . The first thing you need to do - download the stable version AMFPHP (at the time of writing this article is that of version 1.9 ) and unzip the file in / amfphp you can find it at this link.

Now you need to make a little setup. For this:

  • In the /amfphp/globals.php file , in the $ servicesPath and $ voPath variables, specify the full path to the / amfphp / services and amfphp / services / Vo folders, respectively
  • In the file /amfphp/gateway.php we look for an appeal to the method $ gateway-> setCharsetHandler (); and set “none” as the first argument (the other two in this case do not matter)

Default gateway

Now you need to create a getway, to which all AMF requests from the client side will be sent. Of course, you can get by with the standard /amfphp/gateway.php , but you must configure the application (whether it is class initialization, connecting to the database, etc.) before

creating it. We create the /gateway.php file with something like this:

// ROOT directory
define ('ROOT', '/ var / www / html /');
// timezone
date_default_timezone_set ('Europe / Moscow');
// load classes
require ROOT. 'loader.php';
// load amfphp
require_once ROOT. 'amfphp / gateway.php';

Now all boot actions can be done in /loader.php .

Services and VO classes

All services are stored in the / amfphp / services folder . If your project implies MVC architecture (although in our case V is not very relevant), then the services themselves can act as controllers. These controller services can be inherited from any class (this will not affect the mapping), with which you can organize access to models and other methods and / or data of your application. AMF requests of the form ServiceName.MethodName will be sent to the MethodName () method of the ServiceName class, which must be described in the file /amfphp/services/ServiceName.php

VO ( Value Object) classes are classes available for mapping between the AS and the server side of the application. All VO classes must be located in the / amfphp / services / Vo folder . It is worth replacing that the file with the description of the class that came with the AMF request will be loaded automatically, while to create the object on the server side, the file will have to be manually included. But all these questions disappear when using the wonderful __autoload () function:

function __autoload ($ class)
        if (file_exists (ROOT. 'amfphp / services / Vo /'.$ class.'. php '))
                // VO
                require ROOT. 'amfphp / services / Vo /'.$ class.'. php ';
                return true;
            // error
            throw new Exception ('Class \' '. $ class.' \ 'could not be located!');
            return false;

Suppose we need to send the UserDataVO class to the client. To do this, create the file /amfphp/services/Vo/UserDataVO.php with the following contents:

class UserDataVO
    // Explict type
    var $ _explicitType = 'UserDataVO';
    // some user data here ...

And one more important point. If the name of the VO class on the client side contains periods, then amfphp converts from to slashes. This means that the AS class app.Vo.UserDataVO should be described in the file /amfphp/services/Vo/app/Vo/UserDataVo.php as follows:

class UserDataVO
    // Explict type
    var $ _explicitType = 'app.Vo.UserDataVO';
    // some user data here ...

Note that the explict type contains the full name of the class, while the name of the php class itself is only the last part of it.

That's all. I think this article is useful to developers who first encounter AMF in general and AMFPHP in particular. Thanks for attention.

Also popular now: