Installation of AMFPHP
What is AMFPHP?
AMFPHP provides "Flash Remoting" (that's how MacroMedia calls it) for PHP. "Flash Remoting" or for short "Remoting", provides a way for a client application (using Flash) to execute logic on the server (for example a PHP script when using AMFPHP, this can be considered as a "remote service" which has "remote functions"). Flash can execute a remote function with any given parameters and catch the return value of that remote function.
Remoting makes use of a closed protocol AMF (Action Message Format) which is propriarity of MacroMedia. AMF is a closed binary format which makes communication between Flash and remote services very efficient and speedy. Because Data Types are not always exchangable across different scripting languages, a scripting language must describe explicitly for every value it's type. This process is done with serialisation and deserialisation. Comfortably, AMFPHP does this automatically for you. AMFPHP has reverse engineered the AMF format and MacroMedia has no official statment on it but seems to allow it. This is a risc, since MacroMedia might change it's policy towards AMFPHP (especially since MarcoMedia is aquired by Adobe).
When compared to traditional HTML-based browser applications, Flash applications provide unique abilities to create dynamic and sophisticated user interactions, including the following:
- Macromedia Flash Player runtime to execute code, transmit data, and invoke remote services
- Separation of client-side presentation logic from the server-side application logic
- Efficient use of bandwidth provided by the AMF protocol
- Easy deployment on multiple platforms and devices
- On the server side, Flash Remoting runs as a servlet in Java application servers, an assembly in .NET servers, and a native service in ColdFusion MX. Depending on the application server platform, Flash Remoting on the server contains a series of filters that perform logging, error handling, and security authentication, as well as automatically mapping the service function request to the appropriate server technology.
Using Flash Remoting, you can build sophisticated Flash applications, such as a message board, shopping cart, or product catalog.
Install a webserver
The mentioned remote services run on the webserver. In this example we use XAMPP on Windows 2000 Professional to provide an easy way to have a webserver running for development purposes. XAMPP is very insecure by default. If you use a linux server you can also use XAMPP. If you use a linux server we assume you have enough linux skills to understand how to use modify the filepaths mentioned in this article to make it work on linux. If you use VMware to develop in Flash we recommend using a ntp client in both linux as windows to prevent licensing server problems with Flash.
- download XAMPP(http://www.apachefriends.org/en/xampp.html)
- install XAMPP in C:\apachefriends
- open "XAMPP Control Panel" from Desktop or Startmenu and start apache
- make a folder C:\apachefriends\xampp\htdocs\flashservices
Install Flash MX 2004 Professional
In order to use Remoting we cannot just suffice to use a standard installation of Flash MX 20004 Professional. Some extra steps have to be taken care of.
Install AMFPHP
"$gateway->setBaseClassPath("e:/flashservices/services/");"
to:
"$gateway->setBaseClassPath ("c:/apachefriends/xampp/htdocs/flashservices/services/");"
- open a browser window and type: http://yourhostname/flashservices\gateway.php
- you will be asked to download the file. Open the downloaded file. If it's empty this part the installation is so far so good. Otherwise you should get errors now. Troubles troubles section to find solutions.
Create a remote PHP service
- create a new file c:/apachefriends/xampp/htdocs/flashservices/services/HelloWorld.php
- open the file in notepad and copy paste this code:
<?php
class HelloWorld
{
function HelloWorld()
{
$this->methodTable = array(
"sayHelloWorld" => array(
"access" => "remote",
"description" => "Sends back Hello World! to Flash",
"arguments" => array()
)
);
}
function sayHelloWorld()
{
$r=array();
$r[text]="Hello World!";
return $r;
}
}
?>
Create a Flash Remoting script
- open a browserwindow and type: http://yourhostname/flashservices/browser/
- It will show a two frame webpage. The left frame shows available services. HelloWorld should appear.
- click on HelloWorld and in the right frame stubcode appears for ActionScript 2.0 ("Sample AS2 code") and ActionScript 1.0 ("Sample AS1 code"). This ActionScript 2.0 code will be printed:
/**
* This file generated by AMFPHP 1.0
* You can get AMFPHP to generate code customized to your preferences
* By modifying the astemplate.txt file
*/
import mx.remoting.Service;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.RecordSet;
import mx.remoting.DataGlue;
import mx.utils.Delegate;
class HelloWorld
{
//Change the gateway URL as needed
var gatewayUrl:String = "http://yourhostname/flashservices/gateway.php";
var service:Service;
function HelloWorld()
{
mx.remoting.debug.NetDebug.initialize();
this.service = new Service(this.gatewayUrl, null, "HelloWorld", null, null);
}
//Sends back Hello World! to Flash
function sayHelloWorld()
{
var pc:PendingCall = this.service.sayHelloWorld();
pc.responder = new RelayResponder(this, "handleSayHelloWorld", "handleRemotingError");
}
function handleSayHelloWorld(re:ResultEvent)
{
//Implement custom callback code
}
function handleRemotingError( fault:FaultEvent ):Void
{
mx.remoting.debug.NetDebug.trace({level:"None", message:"Error: " + fault.fault.faultstring });
}
}
- copy the "Sample AS2 code" and open Flash MX 2004 Professional
- in Flash choose File-> New -> ActionScript file
- paste the code from clipboard
- replace:
function handleSayHelloWorld(re:ResultEvent)
{
//Implement custom callback code
}
with this:
function handleSayHelloWorld(re:ResultEvent)
{
trace("php output to flash: "+ re.result.text);
}
function handleRemotingError( fault:FaultEvent ):Void
{
mx.remoting.debug.NetDebug.trace({level:"None", message:"Error: " + fault.fault.faultstring });
}
with this:
function handleRemotingError( fault:FaultEvent ):Void
{
trace("an error occured");
mx.remoting.debug.NetDebug.trace({level:"None", message:"Error: " + fault.fault.faultstring });
}
- save the file as c:/apachefriends/xampp/htdocs/flashservices/services/HelloWorld.as
- in Flash choose File -> New -> Flash Document
- Now we set the classpath in Flash: in Flash choose Edit -> Preferences -> Actionscript -> ActionScript 2.0 Settings... click on the "+" button and click on the crosshair button. You should now browse and select c:/apachefriends/xampp/htdocs/flashservices/services
- click on the "OK" button
- inside the "Actions" pane (press F9) paste this code:
import HelloWorld;
h=new HelloWorld();
h.sayHelloWorld();
- make sure the library of your flash document is open (keycombo: Ctrl - L)
- From the Window menu in Flash, select Other Panels > Common Libraries > Remoting
- drag the "RemotingClasses" and "RemotingDebugClasses" inside the library of your Flash Document
- save the file on your Desktop and do Control -> Test Movie (keycombo: Ctrl - Alt - Enter) to compile and run the script
- a window appears "Output Window" and it should show:
php output to flash: Hello World!
Troubles
Conclusion
After closely following this article to the letter and having a succesfully executed remote a sevice called "HelloWorld.php" from a flash script, you have a descent setup and probably have some ideas about how to use AMFPHP for your code. If not please use these resources: