1.定义提供服务的类
Class PersonInfo
{
/**
* 返回姓名
* @return string
*
*/
public function getName(){
return "My Name is Chance";
}
}
?>
2.服务端代码
php
2 //包含提供服务的类进来
3 require_once('PersonInfo.php');
4
5 //wsdl方式提供web service,如果生成了wsdl文件则可直接传递到//SoapServer的构造函数中
6 //$s = new SoapServer('PersonInfo.wsdl');
7
8 //doesn't work 只有location不能提供web service
9 //output:looks like we got no XML document
10 //$s = new SoapServer(null,array("location"=>""));
11
12 //下面两种方式均可以工作,只要指定了相应的uri
13 //$s = new SoapServer(null,array("uri"=>"Server.php"));
14 $s = new SoapServer(null,array("location"=>"","uri"=>"Server.php","actor"=>"getName"));
15
16 $s -> setClass("PersonInfo");
17
18 $s -> handle();
19 ?>
3.客户端代码
php
2 try{
3 //wsdl方式调用web service
4 //wsdl方式中由于wsdl文件写定了,如果发生添加删除函数等操作改动,不会反应到wsdl,相对non-wsdl方式
5 //来说不够灵活
6 //$soap = new SoapClient("");
7
8 //non-wsdl方式调用web service
9 //在non-wsdl方式中option location系必须提供的,而服务端的location是选择性的,可以不提供
10 $soap = new SoapClient(null,array('location'=>"",'uri'=>'Server.php',"actor"=>"getName"));
11
12 //两种调用方式,直接调用方法,和用__soapCall简接调用
13 $result1 = $soap->getName();
14 $result2 = $soap->__soapCall("getName",array());
15 echo $result1."
";
16 echo $result2;
17
18 }catch(SoapFault $e){
19 echo $e->getMessage();
20 }catch(Exception $e){
21 echo $e->getMessage();
22 }
23
24 ?>
阅读(1357) | 评论(0) | 转发(1) |