amfPHP usage

December 11th, 2009

Post in amfPHP

I was asked to share how I use amfPHP.

I usually use a class called Data.as where I store all my website data. In that class I have some public functions, so it’s easy to access information from all other classes.

Here“s a quick example:

Save this as Data.as

package
{
	import flash.net.Responder;
	import flash.net.NetConnection;
	import flash.system.Security;
 
	public class Data
	{
		public static var connection:NetConnection;
		public static var gateway:String = "http://[website]/amfphp/gateway.php";
 
		public function Data()
		{
		}
 
		public static function startAMFPHP():void
		{
			trace("Starting amfPHP Connection");
			connection = new NetConnection();
			connection.connect(gateway);
		}
 
		public static function callService(_functionPHP:String, _functionFLASH:Responder, params:String):void
		{
			trace("Calling amfPHP: " + _functionPHP);
			connection.call(_functionPHP, _functionFLASH, params);
		}
	}
}

Next create a Document Class, called Main.as

package
{
	import flash.display.Sprite;
	import flash.net.Responder;
 
	public class Main extends Sprite
	{
		public function Main()
		{
			Data.startAMFPHP();
			Data.callService("myPHPclassName.myPHPfunctionName", new Responder(aswearFromAMF), "Lorem Ipsum");
		}
 
		private function aswearFromAMF(result:String):void
		{
			trace("this is the aswear from amfPHP");
			trace(result);
		}
	}
}

And finally, this is an example of a amfPHP file. Save it as myPHPclassName.php

<?php
		class myPHPclassName {
 
		public function myPHPfunctionName($myVar)
		{
			return "you said " . $myVar;
		}
	}
?>
amfPHP can be downloaded in: http://www.amfphp.org/