AMFPHP: Flash <–> PHP interaction
Any Flash developer who has worked on RIAs (Rich Internet Applications) that needed to interact with PHP knows that this isn’t a simple process at all. Although AMFPHP has been around for quite a while, I will start with a simple introduction and then build from there in coming articles.
Before AMFPHP was available the only two options to send and load data from PHP were LoadVars and XML. If you wanted to query a database and return a complex data structure then your best option would have been XML since it was more suitable for structuring the data. But even with XML in order to retrieve data from a database you needed the logic that would do the actual query then additional logic that would parse the results of the query and build the xml string and finally parsing the xml on the client side in ActionScript to retrieve the data from the XML object.
You may ask: "So, what’s wrong with that?".
In a complex application debugging and maintenance will become pretty difficult if you have hundreds of PHP scripts just for the sole purpose of generating the xml strings, and several scripts on the client side to parse those xml objects and retrieve the necessary data. Not to mention the waste of time and resources needed to build something on the server side that you will automatically deconstruct on the client side.
Why is AMFPHP better?
Imagine writing a function on the server that queries the database and returns an array i.e.
return mysql_fetch_array(mysql_query("select * from table LIMIT 1"));
Then simply calling that function from ActionScript and getting an array with the results. No need to build XML strings on the server, no need to decode/parse data on the client side. If you send a number you will receive a number, if you send a string you will receive a string, if you send an array you will receive an array, even array keys are preserved. The data transfer is done in the binary AMF format rather then text format. The gateway and the ActionScript classes translate the data into this neutral format, thus allowing language specific data to be exchanged transparently.
If you would like to know more about how AMFPHP works you can check out their official website at http://www.amfphp.org/
Let's get started by setting up a classic "Hello World!" example:
First of all in order to use AMFPHP you will need to install the Adobe Remoting components for Action Script 2. You can download them from: http://www.adobe.com/products/flashremoting/downloads/components/
After installing the remoting components you will need to install AMFPHP. You can download the latest version from: http://www.amfphp.org/
In order to install AMFPHP you will need a web server with PHP installed. You can choose between Apache, IIS, or other web servers. For the sake of simplicity I will assume that you are a Windows XP user and will install IIS.
Installing IIS on Windows XP:
Open your Control Panel:
Click on "Add or Remove Programs";
Click on "Add/Remove Windows Components";
Check the box beside "Internet Information Services (IIS)"
Press the "Next" button.
Your system will ask you to insert the windows installation CD, Insert the CD; press the "OK" button.
And finally after the installation completes press the "Finish" button.
Installing PHP on Windows:
You can download PHP 5.1.4 as a windows installer from:http://php.net/get/php-5.1.4-installer.exe/from/a/mirror
This installer will automatically configure your IIS web server; you just need to follow the steps in the installer.
When the installer prompts you for the installation type select "Standard".
You can choose any directory to install PHP.
You will need to select the version of IIS you installed; if you are a windows XP user your IIS version is 5.1.
If you followed the steps correctly you should have a working web server and PHP installed on your PC.
Let’s set up a test to see if the installations were successful:
Create a new file with the name test.php, open it and add the following code:
Save the file and copy it in your web server’s public directory (C:\Inetpub\wwwroot). Then open your favorite Browser and type the following in the address bar: http://localhost/test.php (or click this link)
If the output you get is similar to the following snapshot your web server and PHP are working properly.
Once your web server and PHP are functional you can start installing AMFPHP. Unzip the contents of the zip archive in your web server’s public web directory (C:\Inetpub\wwwroot)
If you check the contents of the amfphp folder you will notice a gatway.php file this is the gateway all AMFPHP requests will be routed through. You will also notice a services folder this is where your services will reside. A service is a PHP Class; each service will contain the methods you will be calling from ActionScript. If you keep the structure of the AMFPHP package unchanged no further configuration will be needed.
I have created a Connector class that should make the use of AMFPHP easier. By using the connector even developers who didn’t use AS2 before will be able to use AMFPHP. The Connector can also be used with Java OPENAMF (which I will cover in another article), or JRun4.
You can download the Connector class here: http://www.actionscript.com/files/tibi/Connector.zip
Once you downloaded the zip file, create a new flash document and unzip the contents of the Connector.zip archive in the same folder where you flash document is. Add the following code to the first frame of your flash document:
var con:Connector=new Connector(gatewayUrl);
con._PendingCall=con.setService('HelloWorld').say("Hello World from AMFPHP!");
con.setResponders(this.onResult, this.onFault, this);
The gatewayURL is the path to your gateway. I used localhost in this case but you can also use an IP address or domain. The parameter of the con.setService('HelloWorld').say("Hello World from AMFPHP!"); function is the name of the service in which your method is implemented; HelloWorld is the service we will create.
After setting up the service you will call your method, in this case the method name is say con.setService('HelloWorld').say("Hello World from AMFPHP!");
The say method we will implement pings back a message to flash, in this case the message is "Hello World from AMFPHP!"
The onResult and onFault functions are the responders upon a successful AMFPHP call the onResult function will be called upon failure the onFault function will be called. You need to set the responder function in the Connector, in this example the responders are set by con.setResponders(this.onResult, this.onFault, this);
You can give your responder functions any name as long as you pass those names correctly to the connector by using the setResponders function. Now that the client side logic is set up lets create our service.
Create a new PHP file HelloWorld.php and put the following code in it:
methodTable = array
"say" => array
"access" => "remote",
"description" => "Pings back a message"
return 'You said: ' . $sMessage;
Note that the class name must be the same as the file name.
If you aren’t familiar with OOP the first function in the php file is the constructor the name of this function must be the same as the class name.
The constructor contains a methodTable array, all methods contained by the class must be defined here.
And finally our say function, as you can see it simply returns the received parameter.
Save your php file and copy it in the amfphp/services folder on your web server. (C:\Inetpub\wwwroot\amfphp\services)
Now compile your Flash document, you should get a trace output with the following content: "You said: Hello World from AMFPHP!" (You should get the output instantly if the web server is running locally or in a couple of seconds if you are using a web host.)
I hope this article was comprehensive and it helped in giving you a basic idea of what AMFPHP is capable of. In my next article I will explain how to create a simple text chat application with AMFPHP without the use of a database by using persistent PHP sessions.
You can download a zip file containing all the files used in this "Hello World!" application here: http://www.actionscript.com/files/tibi/files.zip(By: Tibor Gyorgy Ballai - ActionScript.com)