myClass.php
<?php
class myClass
{
 /**
 about myClass
 */
 
  function myMethod($var1, $var2, $var3... ){
    return $var1.$var2.$var3 ... ;
  }
}
?>

myFlash.fla
var nc:NetConnection = new NetConnection();
var res:Responder = new Responder(onResult, onFault);
 
nc.connect("http://localhost/amfphp/gateway.php");
nc.call("myClass.myMethod", res, var1, var2, var3, ... );
 
function onFault(responds:Object):void{
 for(var i in responds){
  trace(responds[i]);
 }
}

function onResult(responds:Object):void{
 trace(responds);
}
2010/04/15 22:38 2010/04/15 22:38
2010/04/15 22:38 

在以前寫的一篇文章 amfphp 1.0,第一個 Flash Remoting Demo 作了相同的案例可作為比較,如今 Flash Remoting 已不像以前那麼困難,Hello world 是瞭解一個程式語言的捷徑,在這裡重新示範這個入門的 Flash Remoting 案例。
(以下檔案全部要放置於 amfphp/services 目錄之下)

Helloworld.php

<?php
class Helloworld
{
//給 amfphp 參考的 Class 提示
/**
Say Hello
*/

//Function(也是 Method)
function Say($message){
return 'amfphp say:'.$message;
}
}
?>
需特別注意的是 Class 名稱和檔名需一致,不然會得到 Service does not contain any methods 的錯誤提示。

建立了 Helloworld.php 之後開啟 amfphp 的 Service Browser,我們可以看到剛剛建立的 Class - Helloworld 已經出現在左方列表,點擊它可以看到關於此 class 的資訊,親切的 amfphp 還提供了 PHP 變數測試,在 message 欄位輸入 Hello world! 並按下 call 按鈕。

사용자 삽입 이미지
在 Results 區可看到 "amfphp say: Hello world!" 這就是我們要的結果了。
但 Flash Remoting 可不是只有 PHP 而已喔,接下來透過 Flash 呼叫這個名為 Helloworld 的 PHP Class 才是重頭戲。

Helloworld_AS3.fla
//定義 nc 為網路連線,res 為回應結果及偵錯
var nc:NetConnection = new NetConnection();
var res:Responder = new Responder(onResult, onFault);

//設定此連線透過 amfphp gateway 連結
nc.connect("http://localhost/amfphp/gateway.php");
//設定此連線呼叫項目: call(PHP Class.Method, 回鷹, 變數內容)
nc.call("Helloworld.Say", res, "Hello world!");

//偵錯函數
function onFault(responds:Object):void{
for(var i in responds){
trace(responds[i]);
}
}
//結果函數
function onResult(responds:Object):void{
trace(responds);
}
以上 AS3 寫在第一影格,因 Flash 會自動產生相關 Class,這裡就不寫 import 的部份了...

AS2 的 Flash Remoting 稍有不同,請參考
Helloworld_AS2.fla
var nc:NetConnection = new NetConnection(); 

nc.connect("http://localhost/amfphp/gateway.php");
nc.call("Helloworld.Helloworld.Say", nc, "Hello world!");

nc.onStatus = function(responds:Object) {
for(var i in responds){
trace(responds[i]);
}
}

nc.onResult = function(responds:Object) {
trace(responds);
}


輸出
amfphp say:Hello world!

到這裡 Hello world 的案例已經是完成了,最後參考了 ActionScript3,請 AS 矩形 Say HelloWorld 這篇文章對 Hello world 加上視覺化的改版修正。

[Flash] http://raienet.com/amfphp/services/Helloworld/Helloworld_ui.swf

Helloworld_ui.fla

var nc:NetConnection = new NetConnection();
var res:Responder = new Responder(onResult, onFault);

nc.connect("http://localhost/amfphp/gateway.php");
nc.call("Helloworld.Say", res, "Hello world!");

function onFault(responds:Object):void{
for(var i in responds){
trace(responds[i]);
}
}

function onResult(responds:Object):void{

var msg:String = new String(responds);
var mytxt:TextField = new TextField();
mytxt.border = true;
mytxt.text = msg;
mytxt.x = 135;
mytxt.y = 20;
mytxt.width = 130;
mytxt.visible = false;
addChild(mytxt);

createRect(150,150,100,40,2,0x000000,0xFFCC00);

function clickBtn(myMouseEvent:Event):void{
if (mytxt.visible){
mytxt.visible = false;
}else{
mytxt.visible = true;
}
}

function createRect(x1,y1,w1,h1,borderSize,borderColor,bgColor){
var myButton:Sprite = new Sprite();
myButton.buttonMode = true;
addChild(myButton);
var mc:Shape = new Shape();
mc.graphics.lineStyle(borderSize, borderColor);
mc.graphics.beginFill(bgColor);
mc.graphics.drawRect(x1, y1, w1, h1);
mc.graphics.endFill();
myButton.addChild(mc);
myButton.addEventListener(MouseEvent.CLICK, clickBtn);
}

}

懶得寫註解了...

2010/04/13 22:31 2010/04/13 22:31
2010/04/13 22:31 

看了一些發生在 amfphp 上的設定問題,在使用之前能避開的話,就省事多了。

安裝版本
示範平台:Windows

一、安裝 amfphp
請將壓縮檔裡的 amfphp 1.9 資料夾解開,放到你的 PHP Server 網站根目錄,並將資料夾名稱改為 amfphp

二、設定 amfphp
開啟 amfphp/gateway.php

找到
$gateway->setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");
改成 $gateway->setCharsetHandler("utf8_decode", "UTF-8", "UTF-8");
(使 amfphp 支援中文的方式)

找到 $gateway->disableStandalonePlayer();
改成 //$gateway->disableStandalonePlayer();
(可避免以下錯誤:Error #2044: Unhandled NetStatusEvent: level=error, code=NetConnection.Call.BadVersion)

三、測試 amfphp
開一個新的瀏覽器,在網址列輸入 http://localhost/amfphp/browser *(1)
如果有出現 amfphp 設定訊息就代表安裝成功了。
사용자 삽입 이미지

接著按 Save 即可,這兩個設定已經是最佳設定。
(Encoding: AMF3 這是在大型專案上執行比 XML 還快的技術)
(Set tab after call:Results 呼叫後顯示: 結果)

四、完成
你可以開始使用 amfphp 了,amfphp 相關檔案應置放於以下目錄:amfphp/services *(2)

※ 備註
(1). amfphp 的 Service Browser 位於 amfphp/browser
(2). amfphp 的 網站目錄位於 amfphp/services
2010/04/13 17:23 2010/04/13 17:23
2010/04/13 17:23 

由於費用上的考量,因此選用 amfphp 繼續朝 Flash Remoting 的下個階段前進,為什麼要學 Flash Remoting,一開始學習的動機就是為了想要讀取資料庫,所以在把資料庫弄懂了之後,才會繼續學 Flash Remoting 的其他應用。這個部份要學的是,使用 Flash UI components 的 Datagrid (資料網格元件) 讀取 mySQL 資料庫。

以下以 Raie's Blog 的文章分類作 Flash Remoting DataGrid Demo,先看結果吧。
(PS. 受到 2006/6 Blog 改版影響, 本範例已無法讀到正確數據,請見諒)

[Flash] /attach/1/1111481200.swf

1. 在 flashservices\services\ 之下新增一個檔案夾 labs 以存放測試用檔案,並在裡面再建立一個檔案夾 myRecordset 作為本專案專用檔案夾。

2. myRecordset.php
<? PHP
class myRecordSet
{
function myRecordSet()
{
// 設定資料庫 //主機位置、使用者、密碼
$link = mysql_connect("localhost","myDBUser","myDBPassword");
mysql_select_db ("MyDBName",$link); //資料庫名稱

$this->methodTable = array(
"getRecords" => array(
"description" => "Returns some records",
"access" => "remote",
)
);
}

// 讀取資料
function getRecords()
{
return mysql_query(sprintf("select myColumn1、myColumn2 from myTable order by myColumn1 asc")); //欄位1,欄位2 // 資料表 // 以欄位1排序
}
}
?>

3. 開啟 Flash MX 2004 Professional,選[視窗] > [其他面板] > [內建元件庫] > [Remoting],並將 Remoting.fla 元件庫中的 RemotingClasses 和 RemotingDebugClasses 拖曳到場景中並刪除(這樣此檔案元件庫即可擁有 Remoting 兩個元件)

4. 拖曳 [組件] > [UI Components] > [DataGrid] 和 [Button] 各一個到場景中。並各命名為 Datagrid1 和 Button1;DataGrid 元件可調整大小,而 Button1 可在 [組件檢測器] > [參數] > [label] 輸入按鈕名稱。

5. 在影格上加入以下 ActionScript:
// remoting 相關
import mx.remoting.Service;
import mx.remoting.PendingCall;
import mx.remoting.RecordSet;
import mx.remoting.debug.NetDebug;
import mx.rpc.RelayResponder;
import mx.rpc.ResultEvent;
import mx.rpc.FaultEvent;
import mx.events.*;

// 開始偵錯
NetDebug.initialize();

// 設定 gateway 和 Responder
var gatewayUrl:String = "http://localhost/flashservices/gateway.php"
Responder = new RelayResponder(this、"handleResult"、"handleError");
_service = new Service(gatewayUrl、null、"labs.myRecordSet.myRecordSet"、null、Responder);

// 傳回資料,並將資料置入資料網格
var pc:PendingCall = _service.getRecords(["myColumn1"、"myColumn2"]); //欄位1,欄位2

datagrid1.columnNames = ["myColumn1"、"myColumn2"]; //欄位1,欄位2
datagrid1.getColumnAt(1).width = 100;
datagrid1.getColumnAt(0).headerText = "欄位1";
datagrid1.getColumnAt(1).headerText = "欄位2";
datagrid1.sortableColumns = false;

// 無誤時傳回正確結果
function handleResult(re:ResultEvent):Void{
var mydata:RecordSet = RecordSet(re.result);
datagrid1.dataProvider = mydata;
}

// 有誤時傳回錯誤報告
function handleError(fe:FaultEvent)
{
trace('Error!');
}

// 重新整理按鈕的設定
button1.clickHandler = function(){
datagrid1.removeAll();
var pc:PendingCall = _service.getRecords(["myColumn1"、"myColumn2"]); //欄位1,欄位2
}

6. 將檔案存檔為 Datagrid.fla
7. 開啟 [視窗] > [其他面板] > [NetConnection Debugger] 以準備進行網路連線偵錯。
8. 按 Ctrl + Enter 測試影片,若正確無誤 NetConnection Debugger 將顯示類似以下資訊:
就這樣,讀取 mySQL 的 Flash Remoting Demo 完成了!
2005/09/13 15:42 2005/09/13 15:42
2005/09/13 15:42 

上次介紹過 Flash Remoting 在 Macromedia 官方支援了三個平台:Java、ColdFusion、.NET,然而,為什麼要使用官方不支援的 amfphp 來 Demo Flash Remoting 呢? amfphp 在執行 Flash Remoting 時最大特色在於不必安裝額外模組,只需確定一些設定即可使用,是最容易上手的一種平台。

那麼就開始進行第一個 Flash Remoting 的工作吧!

首先,要先準備好事前工作,除了基本的 Flash MX 2004 Professional 和架設好的 php 平台之外,還需要準備最重要的兩樣配備:

1. Flash Remoting MX Components
2. amfphp 1.0 milestone 2


將 Flash Remoting MX Components 安裝好之後,並在網頁根目錄建立 flashservices 檔案夾(amfphp官方建議),將 amfphp 1.0 milestone 2 所有檔案解壓縮到 flashservices 裡,這樣就算完成初步準備了。

若以上動作無誤,在網址列輸入 http://localhost/flashservices/gateway.php 將會被告知檔案下載訊息。
之後,所有與 Flash Remoting 有關的檔案必須放置在以下目錄:http://localhost/flashservices/services/
接著依 amfphp 官方提供範例,開始進行第一個 Flash Remoting Demo。

1. HelloWorld.php
<?php
class HelloWorld
{
//建構函數
function HelloWorld()
{
$this->methodTable = array
(
"say" => array
(
"access" => "remote",
"description" => "Pings back a message"
)
);
}

// remoting 方法
function say($sMessage)
{
return 'You said: ' . $sMessage;
}
}
?>

2. 開啟 Flash MX 2004 Professional,選[視窗] > [其他面板] > [內建元件庫] > [Remoting],並將 Remoting.fla 元件庫中的 RemotingClasses 和 RemotingDebugClasses 拖曳到場景中並刪除(這樣此文件元件庫即可擁有 Remoting 兩個元件),並在場景中加入一個動態文字,變數設定為 word,並在影格上加入以下 ActionScript:
// remoting 相關
import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;

// 在 NetConnection Debugger 開始顯示偵錯資訊
NetDebug.initialize();

// 設定 gateway
var gatewayUrl:String = "http://localhost/flashservices/gateway.php"
var _service:Service = new Service(gatewayUrl, null, 'HelloWorld', null , null);

//傳回 php 所取得的資訊並作處理
var pc:PendingCall = _service.say("Hello world!");
pc.responder = new RelayResponder(this, "handleResult", "handleError");

// 無誤時傳回正確結果
function handleResult(re:ResultEvent)
{
trace('The result is: ' + re.result);
word = 'The result is: ' + re.result
}

// 有誤時傳回錯誤報告
function handleError(fe:FaultEvent)
{
trace('There has been an error');
}

3. 將檔案存檔為 FirstRemoting.fla
4. 開啟[視窗] > [其他面板] > [NetConnection Debugger] 以準備進行網路連線偵錯。
5. 按 Ctrl + Enter 測試影片,若正確無誤下面三個視窗將各顯示以下資訊:


第一個採用 amfphp 的 Flash Remoting Demo 順利完成了!

附註:Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0包含的 Package 與 Class

 Package 
mx.data.components
mx.remoting
mx.remoting.debug
mx.rpc
mx.services

 Class 
Connection
DataGlue
Fault
FaultEvent
Log
NetDebug
NetDebugConfig
NetServices
PendingCall
RecordSet
RelayResponder
RemotingConnector
Responder
ResultEvent
Service

相關連結:amfphp,Testing your AMFPHP installation
2005/08/29 07:50 2005/08/29 07:50
2005/08/29 07:50