首先到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<br />class HelloWord{<br /> function HelloWord(){}<br /> function sayHello(){<br /> return 'Hello Word';<br /> }<br />}<br />?>
在http://localhost/amfphp/browser/
中會看到你剛建立的檔案,並測試該檔案是否正確。
建立FLEX項目:
建立項目時,要確認是否加入了RPC.SWF模組,並在項目屬性的Flex Complier裡加入 -services "services-config.xml"
並在src下建立services-config.xml檔案
<?xml version="1.0" encoding="UTF-8"?><br /><services-config><br /><services><br /><service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService"<br /> messageTypes="flex.messaging.messages.RemotingMessage"><br /><destination id="amfphp"><br /><channels><br /><channel ref="my-amfphp"/><br /></channels><br /><properties><br /><source>*</source><br /></properties><br /></destination><br /></service><br /></services><br /><channels><br /><channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel"><br /><endpoint uri="http://localhost/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/><br /></channel-definition><br /></channels><br /></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)"><br /> <mx:method name="sayHello" result="onResult(event)"/><br /></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{<br /> trace(evt.result.toString());<br />}