AMFPHP+FLEX简单应用

首先到AMFPHP官方去下载安装包:http://www.amfphp.org/

下载完后,解压文件并把文件放到服务器下。

http://localhost/amfphp/gateway.php 浏览会看到

 

amfphp and this gateway are installed correctly. You may now connect to this gateway from Flash.

Note: If you're reading an old tutorial, it will tell you that you should see a download window instead of this message. This confused people so this is the new behaviour starting from amfphp 1.2.

View the amfphp documentation

Load the service browser

 

显示上面信息,说明安装成功。所有的PHP文件,都要放到amfphp/services/下。

例如创建个MyPHP/HelloWord.php文件:

<?php class HelloWord{ function HelloWord(){} function sayHello(){ return 'Hello Word'; } } ?>

在http://localhost/amfphp/browser/ 中会看到你刚创建的文件,并测试该文件是否正确。

 

创建FLEX项目:

创建项目时,要确认是否加入了RPC.SWF模块,并在项目属性的Flex Complier里加入 -services "services-config.xml"

并在src下创建services-config.xml文件

<?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <service id="amfphp-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://localhost/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/> </channel-definition> </channels> </services-config>

其中<channel ref="my-amfphp"/>的ref必须要于channel-definition 的id一样。

 

创建mxml文件,调用RemoteObject

<mx:RemoteObject id="myService" destination="amfphp"    showBusyCursor="false" source="MyPHP.HelloWord" fault="onFault(event)"> <mx:method name="sayHello" result="onResult(event)"/> </mx:RemoteObject>

  • destination 属性需要与services-config.xml的配置<destination id="amfphp">相对应
  • source 属性会找到amfphp/services/MyPHP/HelloWord.php文件,也就是刚才所创建的PHP文件。
  • 节点<mx:method/>定义调用的方法:name="sayHello"为HelloWord里的sayHello方法,直接对应。             result接受返回值。

private function onResult(evt:ResultEvent):void{ trace(evt.result.toString()); }

 

 

 

你可能感兴趣的:(function,Flex,Class,download,browser,encoding)