用RemoteObject 实现Flex与AMFPHP通信
AMFPHP 下载地址 (http://amfphp.org/) :
http://sourceforge.net/projects/amfphp/files/amfphp/amfphp%201.9%20beta2/amfphp-1.9.beta.20080120.zip/download
解压到web容器目录 (C:\Inetpub\wwwroot)。我在这一步遇到的问题是,直接把amfphp文件夹从压缩包里拖过去后,访问localhost/amfphp/gateway.php 以及 localhost/amfphp/browser ,需要输入本机用户名和密码,并且工作不正常。可能是权限问题。
于是在wwwroot中手动新建一个文件夹,复制amfphp中内容进去,然后删除之前的amfphp,并把新建文件夹改名为amfphp。解决了。
测试: 访问localhost/amfphp/gateway.php 以及 localhost/amfphp/browser
browser这个Flex Application会自动检测C:\Inetpub\wwwroot\amfphp\services 中的php类和方法。
此AMFPHP版本由于PHP版本更新的缘故,需要修改一个文件,替换掉最新PHP不提倡的一个方法。参见:
http://xltank.iteye.com/admin/blogs/583991
更改后,测试通过。
在C:\Inetpub\wwwroot\amfphp\services 中添加PHP类和方法后,可以再browser中的列表里看到。
例如:
test.php
<?php class test { function say($sMessage) { return 'You said: ' . $sMessage; } } ?>
和test1.php
<? class test1{ function test($str){ return $str." end"; } }
*****
配置好AMFPHP后,在
先在C:\Inetpub\wwwroot下新建文件夹(testflex),然后在Flex中新建Flex项目(testflex):
在第一个面板中, Application server type : PHP
第二个面板中,
Web Root : C:\Inetpub\wwwroot\testflex
Root URL :http://localhost/testflex
Output folder : C:\Inetpub\wwwroot\testflex
第三个面板中,
Output folder URL : http://localhost/testflex
完成。
mxml代码:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.managers.CursorManager; import mx.rpc.events.ResultEvent; private function initApp():void { service.say("123333"); service2.test("111111111"); } private function resultHandler(evt:ResultEvent):void { CursorManager.removeBusyCursor(); Alert.show(evt.result.toString()); } private function resultHandler2(evt:ResultEvent):void { CursorManager.removeBusyCursor(); Alert.show(evt.result.toString()); } ]]> </mx:Script> <mx:RemoteObject id="service" showBusyCursor="true" source="test" destination="amfphp"> <mx:method name="say" result="resultHandler(event)" /> </mx:RemoteObject> <mx:RemoteObject id="service2" showBusyCursor="true" source="test1" destination="amfphp"> <mx:method name="test" result="resultHandler2(event)" /> </mx:RemoteObject> <mx:Panel styleName="myPanel" layout="absolute" title="名册" width="200" height="400"> <mx:List id="myList" width="100%" height="100%"></mx:List> </mx:Panel> </mx:Application>
复制C:\Inetpub\wwwroot\amfphp\browser 中的services-config.xml 到Flex项目src中,并修改< endpoint uri =" http://flashservices/gateway.php " 为 < endpoint uri =" http://localhost/amfphp/gateway.php "
在testflex项目的编译参数中添加 -services=services-config.xml。
到这里可以debug了。没问题的话,会弹出两个Alert窗口。