Flash CS4 and AMFPHP
Date: 17 June 2010
AMFPHP is a stable factor in the flash community for communication between server and client-side.
Before AMFPHP flash developers used XML files, GET or POST methods to read and send data to the server.
The late 2003 Wolfgang Hamann reverse-engineered the AMF (Action Message Format) format to craete a working (PHP) gateway. And AMFPHP was born.
This way client applications can directly call PHP class methods. Jut follow the following steps and you'll be ready to use AMFPHP in a couple of minutes.
Installing
- Create an empty directory on your server root (e.g. 'amfphptest', so this will be for instance http://localhost/amfphptest/)- Download the latest AMFPHP build
- Extract the content of the downloaded file into a new subfolder in amfphptest, this will result in the following folder structure:
httpdocs
|_ amfphptest
|_ amfphp
|_ browser
|_ core
|_ ...
- Open gateway.php in your favourite editor and change the following global to "false":// Setting this value to 'false' will allow calling
// the amfphp gateway from your standalone player
define("PRODUCTION_SERVER", false);
- To fire things up open gateway.php in your browser (e.g. http://localhost/amfphptest/amfphp/gateway.php), this will return a message like "amfphp and this gateway are installed correctly. You may now connect to this gateway from Flash."
Creating a simple PHP class
Create a new PHP file with the following content:
methodTable = array
(
"say" => array
(
"access" => "remote",
"description" => "Returns the exact message"
)
);
return $this->methodTable;
}
function say($sMessage)
{
return 'You said: ' . $sMessage;
}
}
?>
Save this file in the amfphp/services/ map under HelloWorld.phpIf you visit the browser (e.g. http://localhost/amfphptest/amfphp/browser/) you'll see the HelloWorld class on your left.
So in short, you just created a simple class with just one method which will return a string containing the first argument.
Let's flash it
Download the small AS3 class (PHPService), which looks like this:
package
{
import flash.net.NetConnection;
import flash.net.Responder;
import flash.net.ObjectEncoding;
import flash.system.Security;
/**
* A simple class to call (AMF)PHP Methods
*
* @author Bas van Kuijck
* @version 1.0
* @since 16-06-10
*/
public class PHPService
{
private var _netConnection:NetConnection;
private var _responder:Responder;
private var _callBack:Function;
public function PHPService($gatewayURI:String)
{
Security.allowDomain($gatewayURI.split('/')[2]);
// Initialize NetConnection
_netConnection = new NetConnection();
_netConnection.objectEncoding = ObjectEncoding.AMF0;
_netConnection.connect($gatewayURI);
// Initialize Responder
_responder = new Responder(_resultHandler, _errorHandler);
}
private dynamic function _resultHandler($result:*):void {
if (_callBack !== null) {
_callBack.call(null, $result);
}
}
private dynamic function _errorHandler($result:*):void {
}
public function call($classMethod:String, $arguments:*= null, $callBack:Function = null):void {
if ($arguments is Function) {
$callBack = $arguments;
$arguments = null;
}
_netConnection.call($classMethod, _responder, $arguments);
if ($callBack !== null) {
_callBack = $callBack;
}
}
public function close():void {
_netConnection.close();
}
}
} This class allows you to call PHP methods through the AMF format by using the NetConnection class.
In order to call (and return) the HelloWorld say method do the following:
import PHPService;
var php:PHPService = new PHPService('http://localhost/amfphptest/amfphp/gateway.php');
php.call('HelloWorld.say', 'test', helloWorldReturn);
function helloWorldReturn($str:String):void {
// This will return "You said: test"
trace($str);
}
That's all it takes, from here on you can adjust the code, create new classes and even send AS3 objects through AMF
Comments