云自無心水自閑

          天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Flex2通過amfphp與PHP的整合調用

          Posted on 2006-06-01 17:33 云自無心水自閑 閱讀(1601) 評論(4)  編輯  收藏 所屬分類: FlexFlex2
          一。安裝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>");


          評論

          # re: Flex2通過amfphp與PHP的整合調用  回復  更多評論   

          2006-06-01 17:36 by dudu
          該文應該放在其他技術區。

          # re: Flex2通過amfphp與PHP的整合調用  回復  更多評論   

          2006-06-06 08:48 by usherlight
          OK.

          # re: Flex2通過amfphp與PHP的整合調用  回復  更多評論   

          2006-08-02 23:32 by Goacross
          不是還要一個數據庫嗎?能否給出完整示例下載?

          # re: Flex2通過amfphp與PHP的整合調用  回復  更多評論   

          2006-08-08 15:26 by usherlight
          create table users {
          id int,
          name varchar(50),
          age int,
          birthday Date
          }
          主站蜘蛛池模板: 黄浦区| 抚顺市| 义马市| 获嘉县| 浠水县| 玉山县| 娄烦县| 巴林右旗| 喀什市| 固镇县| 淅川县| 信丰县| 芜湖县| 上犹县| 九江市| 秦皇岛市| 临江市| 江孜县| 水城县| 蓬莱市| 泊头市| 娄底市| 讷河市| 湾仔区| 南岸区| 墨玉县| 汝阳县| 伊宁市| 肇东市| 防城港市| 磐石市| 茌平县| 水富县| 新沂市| 龙口市| 临海市| 无锡市| 香河县| 鲜城| 青河县| 黄平县|