wikiC/2.06
Main page
Alphabetic index
Recent Edits

install AMFPHP

login 38.107.191.96

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:

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.

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/");"

Create a remote PHP service

<?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


/**
* 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 });
	}
}


 	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 });
	}
import HelloWorld;
h=new HelloWorld();
h.sayHelloWorld();
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:
History of this page