使用PHP搭建WebService服务器

1、WSDL概念

网络服务描述语言是Web Service的描述语言,它包含一系列描述某个web service的定义。

定义模板:


<definitions name='自定义名称[可选]'
targetNamespace='命名空间[一般为URL]'
xmlns:tns='命名空间[值同targetNamespace]'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>

<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="[值同上tns]">
xsd:schema>
types>

<message name='操作名Request'>
<part name="term" type="xsd:string"/>
message>
<message name='操作名Response'>
<part name="value" type="xsd:string"/>
message>

<portType name='操作列表名'>
<operation name='操作名'>
<input message='tns:操作名Request'/>
<output message='tns:操作名Response'/>
operation>
portType>

<binding name='WS下的频道名称' type='tns:频道下的操作列表'>

<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>

<operation name='test'>
<soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>
<input>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
input>
<output>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
output>
operation>
binding>

<service name='WebService名称[如weatherWS,shopWS]'>
<port name='WS下的频道名称[如cartSoap,购物车服务]' binding='tns:[频道名,同左]'>
<soap:address location='http://[webservice地址]'/>
port>
service>
definitions>

2、WSDL实例:


<definitions
targetNamespace='http://localhost/00/'
xmlns:tns='http://localhost/00/'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>

<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://localhost/00/">
xsd:schema>
types>

<message name='testRequest'>
<part name="term" type="xsd:ArrayOfstring"/>
message>
<message name='testResponse'>
<part name="value" type="xsd:ArrayOfstring"/>
message>

<portType name='oplist'>
<operation name='test'>
<input message='tns:testRequest'/>
<output message='tns:testResponse'/>
operation>
portType>

<binding name='cartSoap' type='tns:oplist'>

<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>

<operation name='test'>
<soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>
<input>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
input>
<output>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
output>
operation>
binding>

<service name='shopWS'>
<port name='cartSoap' binding='tns:cartSoap'>
<soap:address location='http://localhost/booledu/webservice/service.php'/>
port>
service>
definitions>

3、搭建Server端

使用php中的soapServer类来搭建WebService服务器


//定义该服务器可以使用的方法
function test($arr){
    return array_reverse($arr);
}
$soap = new soapServer('./wsdl.xml');
$soap->addFunction('test');//注册test方法
$soap->handle();//发布一下
?>

4、搭建Client测试Server端

使用php中的soapClient类来搭建客户端

('Content-type:text/html;charset=utf-8'); $soap = new soapClient('./wsdl.xml'); print_r($soap->test(array('a','b','c')));//测试服务器端搭建的test方法,并传入数组作为参数 ?>

运行结果:

使用PHP搭建WebService服务器_第1张图片

你可能感兴趣的:(WebService)