PHPcode:
yourProject/amf/index.php
<?php defined('PROJECT_ROOT') || define('PROJECT_ROOT' ,realpath(dirname(dirname(__FILE__)))); set_include_path(implode(PATH_SEPARATOR,array( PROJECT_ROOT.'/library' ,get_include_path() ))); require_once 'Zend/Amf/Server.php'; require_once 'Myservice.php'; require_once 'Pop.php'; $server = new Zend_Amf_Server(); $server->setClass('Myservice'); /** * setClassMap * @params $asClass src下的完整路径 * @params $phpClass **/ $server->setClassMap('models.Pop','Pop'); $response = $server->handle(); //handler后一定要echo出去 echo $response;
yourProject/amf/Myservice.php
<?php require_once 'Pop.php'; class Myservice { public function Myservice() {} public function say($str) { return "Hello $str"; } public function getData() { $arr = array(); for($i = 1; $i < 11; ++$i){ $item = new Pop(); $item->pop_id = $i; $item->pop_name = "Dante$i"; $item->str = "中国人"; $arr[] = $item; } return $arr; } public function saveData($item) { $arr = array(); $item->pop_id += 10; $item->pop_name .= '_test'; $item->str .= '_是中国人'; $arr[] = $item; return $arr; } } ?>
yourProject/amf/Pop.php
<?php class Pop { var $pop_id; var $pop_name; var $str; public function Pop() {} } ?>
当PHP搭建好后,可访问http://localhost/yourProject/amf/ ,如果显示
Zend Amf Endpoint
说明PHP已经搭建成功。
Flexcode:
创建你的Flex项目后,右键你的项目Properties->Flex Compiler->Additional compiler arguments:添加
-services "services-config.xml"
yourProject/src/services-config.xml
<?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <service id="zend-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage"> <destination id="zend"> <channels> <channel ref="zend-endpoint"/> </channels> <properties> <source>*</source> </properties> </destination> </service> </services> <channels> <channel-definition id="zend-endpoint" class="mx.messaging.channels.AMFChannel"> <endpoint uri="http://localhost/zend_home/amf/" class="flex.messaging.endpoints.AMFEndpoint"/> </channel-definition> </channels> </services-config>
设置与PHP对应的Model
yourProject/src/models/Pop.as
package models { [Bindable] [RemoteClass(alias="models.Pop")] public class Pop { public var pop_id:int; public var pop_name:String public var str:String; public function Pop() { } } }
yourProject/src/main.mxml
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <!-- RemoteObject source:与PHP的Myservice.php相对应 destination:与services-config.xml中的destination相对应 --> <mx:RemoteObject id="myservice" destination="zend" source="Myservice" fault="faultHandler(event)" showBusyCursor="true"> <!-- Mehotd name:与Myservice.php的方法相对应 --> <mx:method name="say" result="onResult(event);"/> <mx:method name="getData" result="onResult_getData(event);"/> <mx:method name="saveData" result="onResult_saveData(event);"/> </mx:RemoteObject> <mx:Script> <!--[CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import models.Pop; private function faultHandler(evt:FaultEvent):void { trace(evt.fault.faultCode); trace(evt.fault.faultString); trace(evt.fault.faultDetail); } private function onClick():void { var pop:Pop = new Pop(); pop.pop_id = parseInt(txtId.text); pop.pop_name = txtName.text; pop.str = txtStr.text; myservice.saveData(pop); } private function onResult_saveData(evt:ResultEvent):void { dg.dataProvider = evt.result; } private function onResult(evt:ResultEvent):void { trace(evt.result.toString()); } private function onResult_getData(evt:ResultEvent):void { dg.dataProvider = evt.result; var arr:Array = evt.result as Array; var len:int = arr.length; for(var i:uint = 0; i < len; ++i) { var item:Pop = Pop(arr[i]); trace(item.pop_id,item.pop_name,item.str); } } private function dg_c():void { myservice.getData(); } ]]--> </mx:Script> <mx:VBox> <mx:DataGrid id="dg" creationComplete="dg_c();"> <mx:columns> <mx:DataGridColumn dataField="pop_id"/> <mx:DataGridColumn dataField="pop_name"/> <mx:DataGridColumn dataField="str"/> </mx:columns> </mx:DataGrid> <mx:Form> <mx:FormItem> <mx:TextInput id="txtId"/> </mx:FormItem> <mx:FormItem> <mx:TextInput id="txtName"/> </mx:FormItem> <mx:FormItem> <mx:TextInput id="txtStr"/> </mx:FormItem> <mx:FormItem> <mx:Button click="onClick();" label="Click"/> </mx:FormItem> </mx:Form> </mx:VBox> </mx:Application>