flex4.5+php5.2+amfphp通过RemoteObject交互

flex4.5+php5.2+amfphp通过RemoteObject交互
偶尔写写php感觉心情还是蛮舒畅的(Java里的Struts+Hibernate+Spring写久了),写写php才知道,这种被解放的感觉真好。不得不说,php是一种服务器端比较精辟的语言,难怪崇拜者这么多。就来整整flex基于php的交互,看好了,这里要介绍的不是通过flex里面的HttpService组件与php交互,而是借助AMFPHP通过RemoteObject方式来交互。
关于amfphp环境的搭建,请参考本人写的amfphp环境搭建教程,当然里面写的比较粗略,有不清粗的可以联系我。
先来看看php端代码
ProductServices.php
<? php
class  ProductServices{
    
/* *
    *query product list
    
*/
    
function  getProductList(){
        
$link = @ mysql_connect ( " localhost " ,   " root " ,   "" ) or  die ( " Could not connect " );
        
mysql_select_db ( " compass " , $link );
        
mysql_query ( " set names utf8 " , $link );
        
$result   =   mysql_query ( " SELECT * FROM product " , $link );
        
$array = array ();
        
while  ( $row   =   mysql_fetch_array ( $result ,  MYSQL_ASSOC)) {
            
array_push ( $array , $row );
        }
        
mysql_free_result ( $result );
        
mysql_close ( $link );
        
return   $array ;
    }



    
function  findProductById( $id ){
        
$link = @ mysql_connect ( " localhost " ,   " root " ,   "" ) or  die ( " Could not connect " );
        
mysql_select_db ( " compass " , $link );
        
mysql_query ( " set names utf8 " , $link );
        
$result   =   mysql_query ( " SELECT * FROM product where id=  " . $id , $link );
        
$array = array ();
        
while  ( $row   =   mysql_fetch_array ( $result ,  MYSQL_ASSOC)) {
            
array_push ( $array , $row );
        }
        
mysql_free_result ( $result );
        
mysql_close ( $link );
        
return   $array ;
    }

}
?>

在ProductServices.php文件中,定义了一个类ProductServices,里面封装了两个方法,getProductList(),findProductById($id)里面内容很简单,一个是全部查询商品,一个是根据Id查询商品

注意该文件存放的位置C:\inetpub\wwwroot\amfphp\services\ 这样可以被amfphp的资源管理器检索到
 


编写flex端代码
<? xml version = " 1.0 "  encoding = " utf-8 " ?>
< s:Application xmlns:fx = " http://ns.adobe.com/mxml/2009 "  
               xmlns:s
= " library://ns.adobe.com/flex/spark "  
               xmlns:mx
= " library://ns.adobe.com/flex/mx "  minWidth = " 955 "  minHeight = " 600 "
               creationComplete
= " ro.getOperation('getProductList').send() "
               
>
    
<!--  
        ro.getOperation('getProductList').send() 
        ro为RemoteObject的Id
        ro.getOperation('getProductList')获取php文件中的方法名,及要调用服务器端的那个方法
        send()发送请求,在send中可传递参数,多个参数之间用逗号分隔,参数名要与服务器端的参数名一致
    
-->
    
< fx:Declarations >
        
<!--  将非可视元素(例如服务、值对象)放在此处  -->
        
        
< s:RemoteObject id = " ro "  
                        destination
= " amfphp "   
                        source
= " ProductServices "  
                        fault
= " getProductList_faultHandler(event) "  
                        result
= " getProductList_resultHandler(event) "
                        endpoint
= " http://192.168.3.11/amfphp/gateway.php " >
        
</ s:RemoteObject >
        
        
<!--
            RemoteObject中的destination需要与src目录下的services
- config.xml中定义的destination的Id保持一致
            source
= " ProductServices " 要调用服务器端的那个php类,如果存在包的话注意包名.类名
            fault 失败时响应的方法
            result 成功时的方法
            endpoint
= " http://192.168.3.11/amfphp/gateway.php "  正确访问gateway.php的地址
        
-->
        
    
</ fx:Declarations >
    
    
< fx:Script >
        
<! [CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.utils.ArrayUtil;
            
            [Bindable]
            internal 
var  dp:ArrayCollection;
            
            
            
            
// amfphp请求成功时调用方法
            protected  function  getProductList_resultHandler(event:ResultEvent): void
            {
                dp
= new  ArrayCollection(ArrayUtil.toArray(event.result));
            }
            
// amfphp请求失败时调用方法
            protected  function  getProductList_faultHandler(event:FaultEvent): void
            {
                Alert.show(
" 失败了 " ,event.fault.message);                
            }
            
        ]]
>
    
</ fx:Script >
    
    
    
< s:layout >
        
< s:HorizontalLayout />
    
</ s:layout >
    
< s:DataGrid width = " 519 "  height = " 292 "  dataProvider = " {dp} "  requestedRowCount = " 4 " >
        
< s:columns >
            
< s:ArrayList >
                
< s:GridColumn dataField = " id "  headerText = " 编号 " ></ s:GridColumn >
                
< s:GridColumn dataField = " name "  headerText = " 商品名称 " ></ s:GridColumn >
                
< s:GridColumn dataField = " price "  headerText = " 单价 " ></ s:GridColumn >
                
< s:GridColumn dataField = " descption "  headerText = " 描述 " ></ s:GridColumn >
            
</ s:ArrayList >
        
</ s:columns >
    
</ s:DataGrid >
    
</ s:Application >

必须在flex工程的src目录下存放一个名为services-config.xml
<?  version="1.0" encoding="UTF-8" ?>
< services-config >
    
< services >
        
< service  id ="sabreamf-flashremoting-service"
                 class
="flex.messaging.services.RemotingService"
                 messageTypes
="flex.messaging.messages.RemotingMessage" >
            
< destination  id ="amfphp" >
                
< channels >
                    
< channel  ref ="my-amfphp" />
                
</ channels >
                
< properties >
                    
< source > * </ source >
                
</ properties >
            
</ destination >
        
</ service >
    
</ services >

    
< channels >
        
< channel-definition  id ="my-amfphp"  class ="mx.messaging.channels.AMFChannel" >
            
< endpoint  uri ="http://192.168.3.11/amfphp/gateway.php"  class ="flex.messaging.endpoints.AMFEndpoint" />
        
</ channel-definition >
    
</ channels >
</ services-config >

需要将该文件编译到环境中去

效果图

点我下载代码

你可能感兴趣的:(flex4.5+php5.2+amfphp通过RemoteObject交互)