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:

[xml]< ! -- endpoint is the location of the gateway file. source is the name of the service (can be fully qualified like com.mydomain.MyService). AMFPHP does not support named arguments but you can simply write them as arg1, arg2, etc. -->
< mx :RemoteObject endpoint="http://www.mydomain.com/amfphp/gateway.php" id="service" source="MyService" protocol="http" showBusyCursor="true">
< mx :method name="doSomething">
< mx :arguments>
< arg1>{txtName.text}< / arg1>
< / mx:arguments>
< / mx:method>
< / mx:RemoteObject>[/xml]

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:

[as]//usando o AMF0
objectEncoding = ObjectEncoding.AMF0;
//usando o AMF3
objectEncoding = ObjectEncoding.AMF3;[/as]

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

[as]package {
import flash.net.NetConnection;
public class NetConnection2 extends NetConnection {
public function AppendToGatewayUrl(append:String):Void{
//defaultObjectEncoding = AMF0;
}
public function AddHeader():Void{
//
}
public function ReplaceGatewayUrl(append:String):Void{
//ReplaceGatewayUrl
}
}
}[/as]

Having this, we will use another class to use AMF0 and to connect to the AMFPHP
[as]
package {
import flash.display.Sprite;
// import the internal classes
import flash.net.Responder;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
// import your custom class
import NetConnection2;
import flash.util.trace;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;

public class remoting extends Sprite{
public var gateway_conn:NetConnection2;
public var gatewayURL:String = “http://127.0.0.1/flashservices/gateway.php”;
// setup your jazz
public function remoting(){
this.gateway_conn = new NetConnection2();
this.gateway_conn.objectEncoding = ObjectEncoding.AMF0;
this.gateway_conn.connect(gatewayURL);
// make a method call on a PHP class
var r:Responder = new Responder(onPingResult, onPingStatus);
this.gateway_conn.call(“teste2.ola”, r);
}
// call back functions
public function onPingResult(event:Object):Void{
// success!
//trace(event.result);
trace(“onPingResult()”);
var s:Stage = new Stage();
s.scaleMode = StageScaleMode.NO_SCALE;
s.align = StageAlign.TOP_LEFT;
var t:TextField = new TextField();
t.width = 500;
t.text = “FLASH REMOTING!!! eu sou “+event;
addChild(t);
trace(“FLASH REMOTING!!! eu sou ” +event);
}
public function onPingStatus():Void{
//Status;
}
}
} [/as]

Our class in PHP for test.

[php]< ?php //flie teste2.php class teste2{ function teste2(){ $this->methodTable = array(
“ola” => array(
“description” => “testando o AMFPHP com Flex 2.0”,
“access” => “remote” // available values are private, public, remote
)
);
}

function ola(){
return “FLEX 2.0 COM AMFPHP!!!”;
}
}
?>[/php]

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

Was this article helpful? feel free to make a donation and help keep the blog in the air
AMFPHP, Flex

Leave a Reply