Flex 2.0 and AMFPHP!!!

Until Flex 1,5, the AMFPHP could be used normally, simply pointing the address of gateway through tag RemoteObject as in the example a to follow:

  1. < ! -- endpoint is the location of the gateway file. source is the name of the
  2.     service (can be fully qualified like com.mydomain.MyService). AMFPHP does not support
  3.     named arguments but you can simply write them as arg1, arg2, etc. -->
  4. < mx :RemoteObject endpoint="http://www.mydomain.com/amfphp/gateway.php"
  5.    id="service"
  6.    source="MyService"
  7.    protocol="http"
  8.    showBusyCursor="true">
  9.    < mx :method name="doSomething">
  10.       < mx :arguments>
  11.         < arg1>{txtName.text}< / arg1>
  12.       < / mx:arguments>                        
  13.  < / mx:method>
  14. < / mx:RemoteObject>

In Flex 2,0 AMF was introduced a new protocol, AMF3, with improvements of peformance and income. However, to use the RemoteObject he will be need to have the Flex Data Service, that is we are again limited to the requirements of the server for the development.

however as always it was politics of the macromedia, Flex 2,0 still will be compatible with AMF0. Now we have the option to choose between the two protocols. Being between AMF0 and AMF3. This relieved well interesting discussions in the list of flexcoders To determine which protocol he will be used is enough to configure objectEncoding. For example:

ActionScript

  1. //usando o AMF0
  2. objectEncoding = ObjectEncoding.AMF0;
  3. //usando o AMF3
  4. objectEncoding = ObjectEncoding.AMF3;

As result of these discurssões, it was arrived following codigo. This codigo was based on post of Jesse Warden

Creating a class extends the NetConnection class

ActionScript

  1. package {
  2.    import flash.net.NetConnection;
  3.    public class NetConnection2 extends NetConnection {
  4.         public function AppendToGatewayUrl(append:String):Void{
  5.             //defaultObjectEncoding = AMF0;
  6.         }
  7.         public function AddHeader():Void{
  8.             //
  9.         }
  10.         public function ReplaceGatewayUrl(append:String):Void{
  11.             //ReplaceGatewayUrl
  12.         }
  13.    }
  14. }

Having this, we will use another class to use AMF0 and to connect to the AMFPHP

ActionScript

  1. package {
  2.     import flash.display.Sprite;
  3.     // import the internal classes
  4.     import flash.net.Responder;
  5.     import flash.net.NetConnection;
  6.     import flash.net.ObjectEncoding;
  7.     // import your custom class
  8.     import NetConnection2;
  9.     import flash.util.trace;
  10.     import flash.display.MovieClip;
  11.     import flash.display.Stage;
  12.     import flash.display.StageAlign;
  13.     import flash.display.StageScaleMode;
  14.  
  15.     public class remoting extends Sprite{
  16.         public var gateway_conn:NetConnection2;
  17.         public var gatewayURL:String = "http://127.0.0.1/flashservices/gateway.php";
  18.        // setup your jazz
  19.         public function remoting(){
  20.             this.gateway_conn = new NetConnection2();
  21.             this.gateway_conn.objectEncoding = ObjectEncoding.AMF0;
  22.             this.gateway_conn.connect(gatewayURL);
  23.             // make a method call on a PHP class
  24.             var r:Responder = new Responder(onPingResult, onPingStatus);
  25.             this.gateway_conn.call("teste2.ola", r);
  26.         }
  27.         // call back functions
  28.         public function onPingResult(event:Object):Void{
  29.             // success!
  30.             //trace(event.result);
  31.             trace("onPingResult()");
  32.             var s:Stage = new Stage();
  33.             s.scaleMode = StageScaleMode.NO_SCALE;
  34.             s.align = StageAlign.TOP_LEFT;
  35.             var t:TextField = new TextField();
  36.             t.width = 500;
  37.             t.text = "FLASH REMOTING!!! eu sou "+event;
  38.             addChild(t);
  39.             trace("FLASH REMOTING!!! eu sou " +event);
  40.         }
  41.         public function onPingStatus():Void{
  42.             //Status;
  43.         }
  44.     }
  45. }

Our class in PHP for test.

  1. < ?php
  2. //flie teste2.php
  3. class teste2{
  4.  
  5.     function teste2(){
  6.         $this->methodTable = array(
  7.         "ola" => array(
  8.         "description" => "testando o AMFPHP com Flex 2.0",
  9.         "access" => "remote" // available values are private, public, remote
  10.         )
  11.         );
  12.     }
  13.  
  14.     function ola(){
  15.         return "FLEX 2.0 COM AMFPHP!!!";
  16.     }
  17. }
  18. ?>

Now we can sufficiently play with the Flex 2 and AMFPHP.

viem the example

AMFPHP
http://www.amfphp.org/docs/otherplatforms.html

Labs Adobe(Macromedia)
http://labs.macromedia.com

Flexcoders
http://www.flexcoders.com

Get Flash Player 9

ActionScript 3.0, AMFPHP, Flash, Flash Platform, Flash Remoting, Flex, Principal, RIA

Leave a Reply