AMFPHP and Flash Media Server


PHP - AMFPHP - FMS

AMFPHP is a great way to communicate Flex application with server-side scripts. It is probably the easiest way in Flex to receive data from external SQL database – you can find many tutorials about it on the web. AMFPHP can be also used with Flash Media Server, and because it is not as common application, it is a bit hard to find information about it, so I decided to write about it. Let’s prepare a simple FMS application, which will communicate with PHP script.


Step 1 Preparing AMFPHP Service

First of all, we should prepare a simple AMFPHP class with one function, which will return a number and second, that will get a number and write it to a text file. File should be located at amfphp/services directory. Class and PHP file must have the same name.

### /path/to/web/server/amfphp/services/phpTest.php
<?php
        class phpTest
        {
                public function __construct() {                                         // AMFPHP constructor - empty
                }

                function phpGet()
                {
                        $number = rand();
                        return $number;
                }

                function phpPut($number)
                {
                        $f = fopen("sample-filename.txt","w");
                        fwrite($f, $number);
                        fclose($f);
                }
        }
?>
### END OF FILE

Pretty simple, isn’t it?

Step 2 Preparing FMS application

Now, we need to prepare server-side action script for Flash Media Server. Let’s call our application phpTest. First of all, we should create directory named phpTest in applications folder (e. g. /opt/adobe/fms/applications/phpTest). Then, we can create action script file:

### /path/to/fms/applications/phpTest/phpTest.as
var php;                                                                                // this will be our PHP connection
load("netservices.asc");                                                                // we have to load netservices script
application.onAppStart = function()                                                     // we declare function, which will be started on every application startup
{
        NetServices.setDefaultGatewayUrl("http://my.server.name/amfphp/gateway.php");   // we connect to AMFPHP gateway
        this.gatewayconn = NetServices.createGatewayConnection();                       // we create gateway connection
        var amfphpres = new AMFPHPResult(null);                                         // and create result variable
        php = this.gatewayconn.getService("phpTest", amfphpres);                        // here we attach connection to certain AMFPHP class (service)

}

function AMFPHPResult( client )                                                         // function creating object
{
        this.client = client;
}

AMFPHPResult.prototype.phpGet_Result = function( data )                                 // function receiving data from PHP
{
        if (data && (data.length > 0))                                                  // checking, wheter data has been received
        {
                trace(data);                                                            // we send number to debug output
        }
}

AMFPHPResult.prototype.phpPut_Result = function( data )                                 // function called after finishing phpPut function
{
        trace("Done");                                                                  // we confirm running a function to debug output}
}

application.onConnect = function( client, password )                                    // function which is started on every new connection
{
        php.phpPut(application.clients.length);                                         // we store current users number in a file via AMFPHP
        trace("Random number: ");                                                       // and we get random number from PHP
        php.phpGet();
        application.acceptConnection(client);                                           // finally we accept connection
}
### END OF FILE

How can we use this?

Sending data to PHP can be useful to store information about incoming clients (e. g. their IP addresses). Getting information from PHP can be used for determining, whether our machine can handle more connections (by checking server load, etc.). Personally I use it to configure some parameters of my PHP application without need to restart FMS app every time I change them. For last few months this solution proved to be very convenient and reliable.

  1. #1 by Robert at June 4th, 2009

    Czesc!

    Jestem Robert z Wegrzech… Thanks to the post! I will henceforth read this site.

    May I ask you whether you would like to make a short tut about the new flash builder and its integrated zendamf php server support? It could be very useful I think.

    Shortly, you are great! I am very sad because in Hungary there isn’t any Adobe User Group dedicated to the flash technology such your RIAFreaks… :-(

    Anyway, best wishes from Hungarian, Robert

    Ps: my server client (Thunderbird beta3) says that the RSS of the blog is not a valid feed and therefore it could not catch it. Have you got any solution for that?

  2. #2 by Tomasz Orliński at June 5th, 2009

    Hi, Robert!

    Thank you for posting the comment. We fixed RSS address, so you need to change it in your RSS reader, and everything should work fine.

  3. #3 by ubezpieczenie oc motocykla ubezpieczenie oc motocykla kalkulator at November 24th, 2014

    Wszystko w jak najlepszym porządku. Przyjazny kontakt i szybka obsługa CO TO
    JEST UBEZPIECZENIE OC COMPENSA

  4. #4 by window 7 product key at November 30th, 2018

    This is a really helpful post and very informative there is no doubt about it. I found this one pretty fascinating and it should go into my Collection.

  5. #5 by Barbie Crabbs at May 31st, 2019

    Do you want to find the best cure for acne, pimples or other skin issues? Be sure to check out all different methods for the best results that are possible right now

  6. #6 by Randal Senseman at August 14th, 2019

    Thanks for your submission. I also think laptop computers are getting to be more and more popular nowadays, and now in many cases are the only type of computer found in a household. The reason is that at the same time that they’re becoming more and more very affordable, their computing power is growing to the point where they’re as effective as personal computers coming from just a few in years past.

(will not be published)
  1. No trackbacks yet.