2012年3月23日 星期五

AMFPHP研究 : Flash Flex 跟AMFPHP使用

1.amfphp 2.0.1 http://www.silexlabs.org/amfphp/ 下載與教學 (幾個月沒用了 現在已經變成2.1哩)

2.隨便一個可以架站的網站 讓http://localhost/amfphp/Amfphp/index.php 可以找的到即可

在windows下 我是使用appServer 比較簡單易懂
在macos下 我是使用MANP

















3.介紹兩個方式

一個是使用flash NetConnect

一個是使用flex RemoteObject

A.php端


1.建置一個HelloWorld.php
內容如下


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



2.放到資料夾C:\AppServ\www\amfphp\Amfphp\Services 下

3.測試網址http://localhost/amfphp/Amfphp 點選 if you are looking for service browser .....

有出現你放上去的class 還可以測試 就代表你成功了
一般會失敗的原因就是.php file沒有放在 Amfphp/Service下













B.Flash端 


 1.用flash cs 開啟一個fla吧

2.寫script
import flash.net.*;

var con:NetConnection = new NetConnection ;// 新增一個NetConnection物件

var re:Responder = new Responder(onResult,onFault); // 連上AMF gateway

con.connect("http://127.0.0.1/amfphp/Amfphp/index.php");

/*切記要加上 /index.php 
因為他不像flex 只指定http://127.0.0.1/amfphp/Amfphp/ 就好了
不然會一直卡在Error #2044: Unhandled NetStatusEvent:. level=error,code=NetConnection.Call.BadVersion
                  at Untitled_fla::MainTimeline/frame1()
*/
function onResult(result:Object):void {                                            //資料傳送成功時
    var saveData:Object=result;
    trace(saveData);
}
function onFault(fault:Object):void {                                                        //資料傳送失敗時
    trace("failt");
}
con.call("HelloWorld.sayHello",re);

3.即可以看到php裡return Hello World ! (代表成功哩)


C.flex端


1.開啟flash builder new 一個project起來

2.在src 裡 設定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/Amfphp/" class="flex.messaging.endpoints.AMFEndpoint" /></channel-definition>
</channels>
</services-config>
一般都是 http://127.0.0.1/amfphp/Amfphp/ 修改一下 就好了

3.選取Project > Properties > Flex Compiler > Additional compiler arguments 下添加

如圖












4.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
   {
    trace("success");
    result_text.text+=evt.message.body.toString() + "\n"; // 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>

5. run 起來 即可測試看看 是否有收到Hello World !

source code

//使用flash cs6
https://github.com/evonda/testAMFPHP

//使用flash builder 4.6 匯入
https://github.com/evonda/testFlexAmfphp

沒有留言: