Fonkie's CNBLOG

学无止境

 

AMFPhp 与 Flash Builder 4 的交互

1、安装WAMP5(运行PHP的环境)

下载地址:http://www.crsky.com/soft/10723.html  WAMP5 v1.7.4

(本人安装的路径为 C:\wamp\,其中"www"文件内放网站的文件)

2、安装amfphp

下载地址:http://sourceforge.net/projects/amfphp/files/

3、解压后的 amfphp复制到服务器根目录("www"文件夹)下。如:C:\wamp\www\amfphp

4、测试amfphp

如: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

 

 5、PHP端测试例子HelloWorld程序

 在C:\wamp\www\amfphp\services目录,新建一个HelloWorld.php文件,在文件中输入以下代码

 

<?php
class HelloWorld
{
    
function sayHello()
    {
        
return "Hello World!";
    }
}
?>

6、Flex端测试例子AMFphp

 (1)src内,新建一个xml文档:services-config.xml,内容如下:

 

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


(2)AMFphp.mxml

代码
<?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">
    
<s:layout>
        
<s:BasicLayout/>
    
</s:layout>
    
<fx:Script>
        
<![CDATA[
            
            import mx.managers.CursorManager;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            
            private function faultHandler(fault:FaultEvent):void
            {
                CursorManager.removeBusyCursor();
                result_text.text = "code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail;
            }            
            
            private function resultHandler(evt:ResultEvent):void
            {
                result_text.text = evt.message.body.toString(); // same as: evt.result.toString();
            }

        
]]>
    
</fx:Script>
    
<fx:Declarations>
        
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
        
<mx:RemoteObject id="myservice" fault="faultHandler(event)" showBusyCursor="true" source="HelloWorld" destination="amfphp">
            
<mx:method name="sayHello" result="resultHandler(event)" />
        
</mx:RemoteObject>
    
</fx:Declarations>
    
<mx:Button x="250" y="157" label="sayHello" width="79" click="myservice.getOperation('sayHello').send();"/>
    
<mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('foo').send(); "/>
    
<mx:TextArea x="10" y="36" width="319" height="113" id="result_text"/>
    
<mx:Label x="10" y="10" text="Result:"/>
</s:Application>

 

(3)菜单“项目”-“属性”- “flex-编译器"-附加的编译器参数:-locale zh_CN -services services-config.xml

 

7、 服务器根目录下(如:C:\wamp\www)新建一个XML文件命名为crossdomain.xml

<?xml version="1.0" encoding="utf-8"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>

8、在flash builder 4中运行AMFphp.mxml即可看到AMFPhp 与 Flash Builder 4的通信效果

 

 

posted on 2010-07-16 16:42  Fonkie  阅读(783)  评论(0编辑  收藏  举报

导航