一。安裝amfphp,
1. 下載ampfphp1.2版本
2. 建立一個目錄amfphp, 將包中的文件解壓到此目錄中。
3. 目錄結構舉例如下:
c:\amfphp\amf-core
c:\amfphp\browser
c:\amfphp\services
c:\amfphp\gateway.php
在Http Server中(可以是IIS,Apache Http Server)中建立一個虛擬目錄,映射c:\amfphp
4. 驗證
在瀏覽器中輸入 http://localhost/amfphp/gateway.php
會看到一個成功頁面。
二。編寫PHP端代碼
舉個例子:定義一個sample類,這個類編寫在sample.php中
其中定義一個getUsers方法
這個php文件放在amfphp\services\中。
<?php
// Create new service for PHP Remoting as Class
class sample
{
??????? function sample ()
??????? {
??????????????? // Define the methodTable for this class in the constructor
??????????????? $this->methodTable = array(
??????????????????????????????? "getUsers" => array(
??????????????????????????????? "description" => "Return a list of users",
??????????????????????????????? "access" => "remote"
??????????????????????? )
??????????????? );
??????? }
??????? function getUsers ($pUserName) {
??????????????? $mysql = mysql_connect(localhost, "username", "password");
??????????????? mysql_select_db( "sample" );
??????????????? //return a list of all the users
??????????????? $Query = "SELECT * from users";
??????????????? $Result = mysql_query( $Query );
??????????????? while ($row = mysql_fetch_object($Result)) {
??????????????????????? $ArrayOfUsers[] = $row;
??????????????? }
??????????????? return( $ArrayOfUsers );
??????? }
}
?>
驗證:
在瀏覽器中輸入http://localhost/amfphp/browser/index.html
會發現在左邊Frame中有一個sampe的鏈接,點擊后,在右邊Frame中可以測試此方法。
三。編寫Flex端代碼
首先寫一個RemotingConnection類,繼承自NetConnection,主要是用于統一指定編碼格式
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 );
??????????????? }
?????????????? ?
??????????????? public function AppendToGatewayUrl( s : String ) : void
??????????????? {
??????????????? }
??????? }
}
然后在應用中可以進行如下調用:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" creationComplete="initApplication()">
??????? <mx:DataGrid dataProvider="{dataProvider}">
??????????????? <mx:columns>
??????????????????????????????? <mx:DataGridColumn headerText="Userid" dataField="userid"/>
??????????????????????????????? <mx:DataGridColumn headerText="User Name" dataField="username"/>
??????????????????????????????? <mx:DataGridColumn headerText="User Name" dataField="emailaddress"/>
??????????????????????? </mx:columns>
??????? </mx:DataGrid>
??????? <mx:Script>
??????????????? <![CDATA[
??????????????????????? import mx.controls.Alert;
??????????????????????? [Bindable]
??????????????????????? public var dataProvider:Array;
?????????????????????? ?
??????????????????????? import flash.net.Responder;
?
??????????????????????? public var gateway : RemotingConnection;
?????????????????????? ?
??????????????????????? public function initApplication() : void
??????????????????????? {
??????????????????????????????? gateway = new RemotingConnection( "http://localhost/amfphp/gateway.php" );
??????????????????????????????? gateway.call( "sample.getUsers", new Responder(onResult, onFault));
??????????????????????? }
?????????????? ?
??????????????????????? public function onResult( result : Array ) : void
??????????????????????? {
??????????????????????????????? dataProvider = result;
??????????????????????????????? // mx.controls.Alert.show("result: " + result.toString());
??????????????????????? }
?????????????????????? ?
?????????????????????? ?
??????????????????????? public function onFault( fault : String ) : void
??????????????????????? {
??????????????????????????????? // trace( fault );
??????????????????????????????? mx.controls.Alert.show(fault);
??????????????????????? }
??????????????? ]]>
??????? </mx:Script>
</mx:Application>
補充說明:如果調用的PHP函數需要參數,比如:getUsers($user_name)
那么可以在Flex調用端,需要相應的添加此參數:
gateway.call( "sample.getUsers", new Responder(onResult, onFault), "<username>");