现在谈到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 "";
echo "
echo "
echo "
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的信息交互都是这样。所以说不要把交互看的那么难。
本文转自:http://blog.sina.com.cn/s/blog_4909f3cb0100t36h.html