使用PHP搭建WebService服务器

1、WSDL概念

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

定义模板:

<?xml version ='1.0' encoding ='UTF-8' ?>
<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> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型-->
<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="[值同上tns]">
</xsd:schema>
</types>
<!--
<message> 元素可定义每个消息的部件,以及相关联的数据类型.
-->
<message name='操作名Request'>
<part name="term" type="xsd:string"/>
</message>
<message name='操作名Response'>
<part name="value" type="xsd:string"/>
</message>
<!--
<portType> 元素是最重要的 WSDL 元素.它可描述一个 web service、 可被执行的操作,以及相关的消息.
它告诉你去哪个WebService的连接点,扮演了一个控制者.
-->
<portType name='操作列表名'>
<operation name='操作名'>
<input message='tns:操作名Request'/>
<output message='tns:操作名Response'/>
</operation>
</portType>
<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name='WS下的频道名称' type='tns:频道下的操作列表'>
<!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP-->
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
<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>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->
<service name='WebService名称[如weatherWS,shopWS]'>
<port name='WS下的频道名称[如cartSoap,购物车服务]' binding='tns:[频道名,同左]'>
<soap:address location='http://[webservice地址]'/>
</port>
</service>
</definitions>

2、WSDL实例:

<?xml version ='1.0' encoding ='UTF-8' ?>
<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> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型-->
<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://localhost/00/">
</xsd:schema>
</types>
<!--
<message> 元素可定义每个消息的部件,以及相关联的数据类型.
-->
<message name='testRequest'>
<part name="term" type="xsd:ArrayOfstring"/>
</message>
<message name='testResponse'>
<part name="value" type="xsd:ArrayOfstring"/>
</message>
<!--
<portType> 元素是最重要的 WSDL 元素.它可描述一个 web service、 可被执行的操作,以及相关的消息.
它告诉你去哪个WebService的连接点,扮演了一个控制者.
-->
<portType name='oplist'>
<operation name='test'>
<input message='tns:testRequest'/>
<output message='tns:testResponse'/>
</operation>
</portType>
<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name='cartSoap' type='tns:oplist'>
<!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP-->
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
<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>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->
<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服务器

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

4、搭建Client测试Server端

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

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

运行结果:

这里写图片描述

原文地址:https://www.cnblogs.com/cnsec/p/13407039.html