云自无心水自闲

天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 288, comments - 524, trackbacks - 0, articles - 6
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Flex2通过amfphp与PHP的整合调用

Posted on 2006-06-01 17:33 云自无心水自闲 阅读(1591) 评论(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
}

只有注册用户登录后才能发表评论。


网站导航: