一,AS3部分[下载 ]
package { import flash.display.MovieClip; import flash.events.*; import flash.net.*; import flash.text.*; public class Main extends MovieClip { public function Main() { var request:URLRequest = new URLRequest("http://yourdomain.com/index.php?nocache="); request.method = URLRequestMethod.GET; var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.VARIABLES; loader.addEventListener(Event.COMPLETE, completeHandler); loader.load(request); function completeHandler(evt:Event) { b_title.text = evt.target.data.username; b_email.text = evt.target.data.email; } } } }
实现步骤如下:
一,获取一个URLRequest对象实例
var request:URLRequest = new URLRequest("http://yourdomain/index.php?nocache=");
二,指定request对象实现的方法,如GET
request.method = URLRequestMethod.GET;
三,获取一个URLLoader对象实例
var loader:URLLoader = new URLLoader();
四,设置 loader对象的数据格式
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
五,设置一个当获取PHP数据完成后的监听事件
loader.addEventListener(Event.COMPLETE, completeHandler);
六,加载请求
loader.load(request);
简单的说就是先明确请求URL的属性,如路径地址,请求方式等,然后设置返回数据的实现方式,如数据格式,处理方法等
二,PHP部分[路径为http://yourdomain/index.php这 个可根椐实际情况进行修改]
<?php $value = "username=myname&[email protected]"; echo $value; ?>
注意数据的组装形式,类似于参数=值,如果有多个参数使用&分割
如上实例,就可以实现一个简单的使用FLASH AS3远程获取PHP数据的功能。有了这些知识,就可以开始实现一个简单的基于AS3+PHP的网站。
点我下载Flash AS3获取远程PHP数据实例源代码
http://jquerycodes.googlecode.com/files/as3_php.rar
完整实例2 (发送邮件)
首先在属性中绑定 Main 类
package { import flash.display.*; import flash.events.*; import flash.text.*; import flash.net.*; public class Main extends MovieClip { public function Main() { addEventListener(Event.ADDED_TO_STAGE,init); } private function init(evt:Event):void { removeEventListener(Event.ADDED_TO_STAGE,init); trace("My Document Class has been created correctly"); form_mc.send_btn.addEventListener(MouseEvent.MOUSE_DOWN,checkFields); } private function checkFields(evt:MouseEvent):void { if(form_mc.name_txt.text!=""&&form_mc.email_txt.text!=""&&form_mc.message_txt.text!="") sendMessage(); } private function sendMessage():void { var variables:URLVariables=new URLVariables(); variables.name=form_mc.name_txt.text; variables.email=form_mc.email_txt.text; variables.message=form_mc.message_txt.text; var request:URLRequest=new URLRequest(); request.url='email.php'; request.method=URLRequestMethod.POST; request.data=variables; var loader:URLLoader=new URLLoader(); loader.dataFormat=URLLoaderDataFormat.VARIABLES; loader.addEventListener(Event.COMPLETE,messageSent); try { form_mc.status_txt.text = 'waiting...'; loader.load(request); } catch (error:Error) { form_mc.status_txt.text = 'Unable to load requested document.'; } } private function messageSent(evt:Event):void { var loader:URLLoader=URLLoader(evt.target); var vars:URLVariables=new URLVariables(loader.data); if(vars.answer=="ok"){ form_mc.name_txt.text = ""; form_mc.email_txt.text = ""; form_mc.message_txt.text = ""; form_mc.status_txt.text = "The message has been sent"; }else{ form_mc.status_txt.text = "Something wrong"; } } } }
附送ActionScript2 的php交互方式:
/* LoadVars load example */ // init LoadVars Object lv = new LoadVars(); // define onLoad Callback lv.onLoad = onLoadCallBack; // send and load variables lv.load("http://localhost:2400/lv.txt?" + new Date()); // onLoad Callback function onLoadCallBack(succes) { // if succes if(succes) { // trace variables trace(this.lVar1); trace(this.lVar2); } else { // loading failed trace("Loading Error!!"); } } /*LoadVars send example*/ // init LoadVars Object lv = new LoadVars(); // set Variables lv.sVar1 = "value1"; lv.sVar2 = "value2"; // define onLoad Callback lv.onLoad = onLoadCallBack; // send and load variables lv.sendAndLoad("http://localhost:2400/lv.php?" + new Date(), lv, "POST"); // onLoad Callback function onLoadCallBack(succes) { // if succes if(succes) { // trace variables trace(this.lVar1); trace(this.lVar2); } else { // loading failed trace("Loading Error!!"); } } /* PHP code */ <? // get variables $var1 = $_POST['sVar1']; $var2 = $_POST['sVar2']; // send variables echo "&lVar1=$var1 returned&"; echo "&lVar2=$var2 returned as well&"; ?>