as3 与php通信,Flash AS3与php通信

这篇博客介绍了AS3(ActionScript 3)与PHP进行数据交互的四种常见方法:(1)通过URLVariables获取变量,(2)解析XML响应,(3)使用GET传递参数,(4)通过POST方法传递参数。这些方法展示了基本的客户端-服务器通信流程,对于理解AS3与PHP的交互很有帮助。
摘要由CSDN通过智能技术生成

现在谈到AS3与PHP的交互,第一反应都会想到AMF

其实AMF也不过是利用PHP或者其他语言来写的一个信息后台罢了,

回归到原始,无非通信还是这几种方法。

(1)直接读取

php:

$state="开始接收";

$var1="收到";

echo "state=".$state."&var1=".$var1;

?>

as:

//btn是个按钮 txt是动态文本,在flash里面直接创建就OK

btn.addEventListener(MouseEvent.CLICK,loadMss);

txt.text="loading...";

function loadMss(e:MouseEvent):void

{

var urlLoader:URLLoader=new URLLoader();

mssLoader.dataFormat=URLLoaderDataFormat.VARIABLES;

mssLoader.load(new URLRequest("http://localhost/as3/admin.php"));//这里就是php文件的地址

mssLoader.addEventListener(Event.COMPLETE,completeFun);

}

function completeFun(e:Event):void

{

var loadData:URLVariables=URLVariables((e.currentTarget as URLLoader).data);

txt.text="正在:"+loadData.state+"\n";

txt.text+="接收情况:"+loadData.var1;

}

(2)读取PHP生成的xml

php:

//这里只是简单的echo出来了

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

echo "";

echo "1.jpg";

echo "2.jpg";

echo "";

?>

as:

//btn是个按钮 txt是动态文本,在flash里面直接创建就OK

btn.addEventListener(MouseEvent.CLICK,loadMss);

function loadMss(e:MouseEvent):void

{

var urlLoader:URLLoader=new URLLoader();

xmlLoader.load(new URLRequest("http://localhost/as3/xml.php"));//这里就是php文件的地址

xmlLoader.addEventListener(Event.COMPLETE,completeFun);

}

function completeFun(e:Event):void

{

var loadData:XML=XML((e.currentTarget as URLLoader).data);

txt.text=loadData.toString();

}

(3)通过GET传出参数

//btn是个按钮 txt是动态文本,在flash里面直接创建就OK

System.useCodePage=true;

btn.addEventListener(MouseEvent.CLICK,loadMss);

function loadMss(e:MouseEvent):void

{

var getLoader:URLLoader=new URLLoader();

var request:URLRequest=new URLRequest();

request.url="http://enatool.com/something.php";//这里是接收参数的地址

request.method=URLRequestMethod.GET;//传出方法

request.data="s=1";//传出具体的信息

getLoader.load(request);

getLoader.addEventListener(Event.COMPLETE,completeFun);

}

function completeFun(e:Event):void

{

txt.text=(e.currentTarget as URLLoader).data;

}

(4)通过POST传参

//btn是个按钮 txt是动态文本,在flash里面直接创建就OK

System.useCodePage=true;

btn.addEventListener(MouseEvent.CLICK,loadMss);

function loadMss(e:MouseEvent):void

{

var postLoader:URLLoader=new URLLoader();

var request:URLRequest=new URLRequest();

var vars:URLVariables=new URLVariables();

vars.s1="flash";

vars.s2="flex";

request.url="http://enatool.com/something.php";

request.method=URLRequestMethod.POST;

request.data=vars;//这里的data可以是一个Object,或者Array

postLoader.load(request);

postLoader.addEventListener(Event.COMPLETE,completeFun);

}

function completeFun(e:Event):void

{

txt.text=(e.currentTarget as URLLoader).data;

}

和php或者类似asp,.net的信息交互都是这样。所以说不要把交互看的那么难。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值