【IT168 技术文档】
借助于CodePlex上开源项目PHP for Microsoft AJAX Library的帮助,我们已经可以在PHP上使用ASP.NET AJAX的很多核心功能了。
下载安装
PHP for Microsoft AJAX Library目前仅仅处于Alpha阶段,想实际使用似乎还早了点,只能尝鲜了。
预先需求有PHP 5.2版本,且必须安装了php-json模块。
下载地址:http://www.codeplex.com/phpmsajax/Release/ProjectReleases.aspx?ReleaseId=1692
安装方法:
下载PHP for Microsoft AJAX Library并解压缩
下载Microsoft AJAX Library(http://ajax.asp.net)
在PHP Web Service代码中include一下MSAjaxService.php。
在调用该Web Service的页面中,引入MicrosoftAjax.js文件。
下面来看一个“经典”的场景:调用服务器端方法取得复杂类型。
编写Service文件
新建一个php文件,命名为EmployeeService.php。首先写上这一句,include必要的支持代码:
require_once 'MSAjaxService.php';
然后定义一个Employee类。四个属性一目了然,不用多说:
接下来是EmployeeService类,继承于MSAjaxService.php中的MSAjaxService基类。其中定义一个方法,用来返回一个Employee对象:class Employee
{
public $Id;
public $Name;
public $Email;
public $Salary;
![]()
function __construct($id, $name, $email, $salary)
{
$this->Id = $id;
$this->Name = $name;
$this->Email = $email;
$this->Salary= $salary;
}
}
然后新建一个EmployeeService的实例,并且调用基类的ProcessRequest()方法,处理该请求:class EmployeeService extends MSAjaxService
{
function GetEmployee()
{
return new Employee(12345, "Dflying Chen", "Dflying@some.com", 1000);
}
}
$theService = new EmployeeService();
$theService->ProcessRequest();
![]()
