您的位置:首页 > 编程语言 > PHP开发

zend_amf与actionscript通信的实例

2011-04-27 13:52 120 查看
1、我们新建一个zend framework project

2、在application下面添加一个services文件夹,在此文件夹中新建一个php文件,Index.php,此文件的类名为:Service_Index,代码如下

class Service_Index

{       

   

}

现在我的项目目录如下如所示:



3、在IndexController类的indexAction里面添加如下代码

        $server = new Zend_Amf_Server();

        $server->setClass("Service_Index");

        echo $server->handle();

        exit;

如果想要测试你的代码是否正确。将$server->handle()前的echo去掉,访问页面http://localhost/amftest/public/,可以看到页面上的输出字符串“Zend Amf Endpoint”,这就说明我们的Zend_Amf_Server编写成功了。

需要注意的是,如果想要php和actionscript通信,则需要加上$server->handle()之前的echo.

4、此时我们编写一个方法与actionscript交互

在Service_Index类里面加入如下代码

public function hello() {

        return "hello world";

}

5、新建一个flash文件,在界面上添加一个动态文件,取名为“myText”,在第一帧中加入如下代码

import flash.net.Responder;

import flash.net.NetConnection;

var mynet:NetConnection = new NetConnection();

mynet.connect("http://localhost/amftest/public/index/index");

mynet.call("Service_Index.hello",new Responder(onOk, onNotOk));

function onOk(e:String){

    myText.text = e;

}

function onNotOk(){

    myText.text = "failer";

}

保存,运行,我们就可以在界面上看到“hello world”了。

6、至此我们的zend_amf和actionscript之间的通信的一个最简单的实例就算成功了。

7、源码下载见此链接:http://download.csdn.net/source/3230046
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息