最新消息: 新版网站上线了!!!

php做Web Service开发入门实例

一个php做Web Service开发入门实例,代码如下:

addclass.php 这个是要调用的类文件,就是处理数据的核心

<?php
class addclass{
function add($x,$y){
return $x+$y;
}
}
?>

 

soapserver.php 此文件做服务端软件,让客户端请求

<?php
require (‘./addclass.php’);
$soap = new SoapServer(“./test.wsdl”);
$soap->setClass(“addclass”);
$soap->handle();
?>

 

soapclient.php 客户端请求示例

<?php
$client = new SoapClient(“http://www.com/api/test.wsdl”);
echo $client->add(7,8);
?>

 

还有一个test.wsdl文件,这个可以用zend studio直接生成,

说明:

《1》建立了类文件后就要进行生成 swdl文件了,呵呵!当然不能手写了,用zend工具生成

流程:

Tools ==> WSDL Generator ==> Configration name : test; WSDL file name: test ==>NEXT ==> 点击 +   ==> 选择上面的创建的class.php ==> 这时候会看到一个 classes   : URL Location的映射,保留 test类前面的勾,并将其url 设置为 SOAP Server的url:http://localhost:8080/server.php ==>点击Finish,ZDE就会创建一个非常漂亮的WSDL了   工作基本上完成了.

这个swdl生成非常重要,必须按步骤来!

《2》类中的每个变量之前不需要加上类似的下面这种格式,这样生成swdl文件时类型才不会出错。

/*
@param string $msg
*/

当然生成swdl文件后,要把它拷在documentroot 下。

WEB SERVICE就这么简单.

转载请注明:谷谷点程序 » php做Web Service开发入门实例