AMFPHP in AS3
Remoting in ActionScript 3 is easy to implement, but there are some important things to keep in mind. Most notably, the mx.remoting package is no longer required. There are no additional packages to install, everything you need is built right in to flash.net
This RemotingConnection class extends NetConnection and sets the objectEncoding to AMF:
package
{
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
public class RemotingConnection extends NetConnection
{
public function RemotingConnection( sURL:String )
{
objectEncoding = ObjectEncoding.AMF0;
if (sURL) connect( sURL );
}
}
}
This class employs the above class, and makes a remote service call:
package
{
import flash.net.Responder;
public class AS3RemotingTest
{
public var dataProvider:Array;
public var gateway : RemotingConnection;
public function AS3RemotingTest()
{
this.init();
}
public function init()
{
var gatewayUrl:String = "http://localhost:88/amfphp/gateway.php";
gateway = new RemotingConnection(gatewayUrl);
var responder:Responder = new Responder(onResult, onFault);
var arg:String = 'foo';
gateway.call( "HelloWorld.say", responder, arg);
}
public function onResult( result ) : void
{
trace('onResult invoked');
trace(result);
}
public function onFault( fault : String ) : void
{
trace('onFault invoked');
trace( fault );
}
}
}
(Note: The above code assumes that you have AMFPHP installed and running out of a folder named amfphp on your localhost)
Digg It | Add to Del.icio.us

I worked with amfphp and as2 but now that I’am working with flex 2 and as3 I was wondering if it was possible to use amfphp for that to. Well her was the answere, thnx.
Comment by Sander Lissenburg — November 10, 2006 @ 12:08 pm
Hey Caleb,
Thanks for making it so ‘cut and dried’, worked the first time.
Aaron
Comment by Aaron — February 28, 2007 @ 8:20 pm
The latest version of AMFPHP (currently 1.9beta) supports AMF3 so that RemoteObject, etc. will work with Flex 2.
Comment by Ville — March 17, 2007 @ 2:44 pm
I’ve got a free class package that revives a lot of the AS2 conversion functionality for recordSets, and strict types result sub-objects so they’re a lot easier to work with than just the Objects thrown back by the NetConnection.call method. It also provides an in-movie mini version of the old NetConnection Debugger if you like to see your results that way. It’s free to all and you can get it at:
http://www.joshstrike.com/strike_remoting.zip
Cheers.
Comment by Josh Strike — May 5, 2007 @ 12:00 pm
Hi, firstly thanks for the tutorial. I’m trying to convert my old AS2 code by using your new AS3 code and am having a problem with one line in particular. When I compile the code I get an error at the following line…
var responder:Responder = new Responder(onResult, onFault);
The error being…
1136: Incorrect number of arguments. Expected 0.
Comment by KJ — September 14, 2007 @ 3:24 am
Sorry, ignore that, I made a stupid error.
Thanks!
Comment by KJ — September 14, 2007 @ 3:33 am
Hi, I wrote a package called AMFPHPService (plus a helper class called DisplayMessage in ActionScript 3.0, plus an XML file called messages_en.xml containing error messages) to make calls to AMFPHP. Inspired by Josh Strike’s phpRemoting stuff, but different (I don’t like deriving from the sprite class) Download available at http://www.j-c-s.co.uk/flashremoting/AMFPHPService.zip
Comment by Georg Jordt — October 6, 2007 @ 2:24 am
Georg,
Thanks for sharing! I haven’t checked out your classes yet, but I most certainly will soon. I will follow up via email after doing so.
At any rate, I appreciate your comment
Comment by Caleb — October 6, 2007 @ 3:15 am
Thanks, this was really helpful and time-saving. Very clear and nice script.
Comment by Frederik — November 21, 2007 @ 3:05 pm
Nice work, Georg. Very clean, light way of doing things. I’ve struggled to bring down the overhead with mine. Nice to see people coming up with new solutions, and thanks for the undeserved credit!
Comment by Josh Strike — March 1, 2008 @ 3:38 am
I´m a beginner interested in Georg’s class.
Anyone please can bring us an example, I can´t make it work, don´t know what I’m doing wrong.
Thanks a lot !
Comment by Ricardo — August 22, 2008 @ 5:56 am