AS3 AMFPHP Example

The following is the AS3 code to connect to Drupal site running Services (2x) & AMFPHP server to retrieve a specific selection of nodes created within views.

You will need to create 2 files

1. "Main.as" ->

package {
       
        import flash.display.Shape;
        import flash.display.SimpleButton;
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import flash.net.NetConnection;
        import flash.net.Responder;
        import flash.text.TextField;
       
        public class Main extends Sprite
        {
               
                private var gateway:NetConnection;
                private var txt:TextField;
                private var btn:SimpleButton;
               
                public function Main()
                {
                        createSimpleView();
                }
               
                public function init():void {
                       
                        gateway = new NetConnection();
                       
                        gateway.connect("http://localhost/services/amfphp");
                       
                        var responder:Responder = new Responder(onResult, onFault);
                       
                        var arg:Array = new Array();
                       
                        gateway.call( "views.get", responder, 'techblog_all','',arg, 0, 10);      
                       
               
                }
               
                public function onResult(responds:Object):void {
                        txt.text = "onResult: \n";
                        reflect(responds);
                }
               
                public function onFault(responds:Object):void {
                        txt.text = "onFault : " +responds.error ;
                        reflect(responds);
                }
               
                public function createSimpleView():void {
                        txt = new TextField();
                        txt.x = txt.y = 10;
                        txt.width = 390;
                        txt.height = 200;
                        txt.border = true;
                       
                        addChild(txt);
                       
                        btn = new SimpleButton();
                        btn.x = 10;
                        btn.y = 220;
                       
                        btn.upState = createRectangle( 0x00FF00 );      
                        btn.overState = createRectangle( 0xFF0000 );
                        btn.downState = createRectangle( 0x0000FF );
                        btn.hitTestState = btn.upState;
                        btn.addEventListener( MouseEvent.CLICK, btnClick );      
                        addChild(btn);        
                }
               
                private function btnClick(event:MouseEvent):void {
                        init();
                }
               
                private function createRectangle(color:uint):Shape {      
                        var rect:Shape = new Shape(  );
                        rect.graphics.lineStyle( 1, color );
                        rect.graphics.beginFill( color );
                        rect.graphics.drawRect(0,0,150,50)
                        rect.graphics.endFill();  
                        return rect;
                }
               
                private function reflect(obj:Object):void{
                        for(var i:String in obj){
                                txt.appendText("\n"+i+": "+obj[i].node_title);
                        }
                }
        }
}

2. "RemotingConnection.as" ->

package
{
        import flash.net.NetConnection;
       
        import flash.net.ObjectEncoding;
       
        public class RemotingConnection extends NetConnection
        {
                public function RemotingConnection( sURL:String )
                {
                        objectEncoding = ObjectEncoding.AMF3;
                        if (sURL) connect( sURL );
                }
        }
}




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.
Bg