时间:2021-07-01 10:21:17 帮助过:41人阅读
import mx.controls.Alert;
import mx.rpc.remoting.mxml.RemoteObject;
import mx.rpc.events.*;
public var login_remoteObj:RemoteObject = null;
public function initLoginRemoteObject():void
{//初始化RemoteObject
this.login_remoteObj = new RemoteObject();
this.login_remoteObj.source = "Login";
this.login_remoteObj.destination = "amfphp";
this.login_remoteObj.showBusyCursor = true;
this.login_remoteObj.endpoint = "http://localhost/MyTest/amfphp/gateway.php";
this.login_remoteObj.doLogin.addEventListener("result", loginHandler);
this.login_remoteObj.doLogin.addEventListener("fault", faultHandler);
}
public function doLogin():void
{//登陆操作,向服务器提交数据
var name:String = this.txtName.text;
var pwd:String = this.txtPassword.text;
var data:Array = new Array();
data.push(name);
data.push(pwd);
this.login_remoteObj.getOperation("doLogin").send(data);
}
public function loginHandler(event: ResultEvent):void
{//处理服务器返回的结果
var result:Array = event.result as Array;
var flag:String = result[0];
if (flag == "0") {
Alert.show("登陆失败: " + result[1]);
} else if (flag == "1") {
Alert.show("登陆成功: " + result[1]);
} else if (flag == "-1") {
Alert.show("异常: " + result[1]);
}
}
public function faultHandler(event: FaultEvent):void
{//出错处理
Alert.show("sorry,出错了!!!");
}
}
二,PHP服务器端
1,将amfphp文件夹置于MyTest项目的根目录下,打开浏览器输入下述地址验证amfphp是否安装成功
代码如下:
http://localhost/MyTest/amfphp/gateway.php
amfphp就是通过这个gateway来定位我们的服务类,并将请求转发给这些服务类进行处理的。
2,Login.php文件,包含了处理登陆请求的Login类,此文件置于BusinessLogic目录下
代码
代码如下:
<?php
class Login
{
public function doLogin($data)
{
$result = array();
try {
$name = array_shift($data);
$pwd = array_shift($data);
if ($name == "phinecos" && $pwd == "123") {
$result[] = "1";
$result[] = "you are valid user!";
} else {
$result[] = "0";
$result[] = "login failed";
}
} catch (Exception $ex) {
$result[] = "-1";
$result[] = $ex->getMessage();
}
return $result;
}
}
?>
3,将globals.php中的服务路径项修改如下,为amfphp指明服务类所在的目录
代码如下:
$servicesPath = "../BusinessLogic/";
作者:洞庭散人
AMFPHP 下载地址