As3利用AMFPHP與PHP進展通訊

來源:互聯網
上載者:User
As3利用AMFPHP與PHP進行通訊

來自:http://bbs.9ria.com/thread-65836-1-1.html

?

首先,說下具體配置,PHP版本:5.2.5,apache:2.2.11,(或者WAMP2.2),amfphp:1.9版本(附件已提供),windows xp系統。

注意:amfphp的不同的版本與php不同的版本會有衝突。具體檢測方法是:在web根目錄下解壓amfphp後,訪問該url: http://localhost/amfphp/browser/,如果沒有報任何錯誤,那麼恭喜你。如果有錯誤,請更換amfphp的版本。

經我的測驗,上面那個配置是不會有問題的。

在具體的項目中,有時我們會使用As3純項目,所以在As3中Amf通訊也經常用到,今天我們來講講在As3中的方法。(以前已經發過flex與amfphp通訊的相關文章,請查閱後台板塊)
在As3端,主要就是NetConnection的使用,首先選擇使用AMF3協議,這個是現在常用的,AMF0因為要相容以前的版本,所以有些冗餘。
然後使用connect方法串連後台url。
然後聲明一個Responder對象,該對象在 NetConnection.call() 中使用以處理來自與特定操作成功或失敗相關的伺服器的傳回值。
接著使用NetConnection,call 後台php端類中的方法,如本例的HelloWorld.getData,記著把是所聲明的 Responder對象寫到call方法的第2個參數裡。

注意看Responder裡兩個函數參數,第一個,如果對伺服器的調用成功並返回結果,則此函數被調用。例子中的onSuccess
第二個, 如果伺服器返回一個錯誤,則此函數被調用。例子中的,onError。

好,來看看As3端:

package  {    import flash.display.Sprite;    import flash.events.NetStatusEvent;    import flash.net.NetConnection;    import flash.net.ObjectEncoding;    import flash.net.Responder;    /**     * ...     * @author 納蘭容若     */    public class AMFTest extends Sprite{        private var netCon:NetConnection = new NetConnection();        private var rsp:Responder = new Responder(onSuccess, onError);        private var phpUrl:String = "http://localhost/amfphp/gateway.php";        public function AMFTest() {            netCon.objectEncoding = ObjectEncoding.AMF3;            netCon.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);            netCon.connect(phpUrl);            netCon.call('HelloWorld.getData', rsp);        }        private function onNetStatus(e:NetStatusEvent):void {            trace("failed");        }        private function onSuccess(rspObj:Object):void {            trace("ok: "+rspObj);        }        private function onError($rspObj:Object):void {            trace("AMF client error.");        }    }}

?

php端:


php檔案命名為 HelloWorld.php,放到 D:\webroot\amfphp\services中。(注意:D:\webroot是我的web根目錄,這個具體設定要看你自己的。)

如果連結出現問題,則有可能是如下錯誤導致的:

?

Fatal error: Uncaught exception 'VerboseException' with message 'Standalone Flash player disabled. Update gateway.php to allow these connections' in C:\wamp\www\amfphp\core\amf\app\Gateway.php on line 357 VerboseException: Standalone Flash player disabled. Update gateway.php to allow these connections in C:\wamp\www\amfphp\core\amf\app\Gateway.php on line 357 Call Stack: 0.0003 374432 1. {main}() C:\wamp\www\amfphp\gateway.php:0 0.0062 874840 2. Gateway->disableStandalonePlayer() C:\wamp\www\amfphp\gateway.php:137 0.0062 875056 3. trigger_error() C:\wamp\www\amfphp\core\amf\app\Gateway.php:357 0.0062 875720 4. amfErrorHandler() C:\wamp\www\amfphp\core\amf\app\Gateway.php:357

?

此時只需要按提示,開啟gateway.php檔案,屏蔽掉原135、137行即可。

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.