使用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方法,并传入数组作为参数 ?>

运行结果:

这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
搭建WebService的过程可以分为以下几个步骤: 1. 创建WebService接口:首先需要定义一个接口,该接口包含WebService的方法。在引用\[3\]中的示例中,定义了一个名为WeatherService的接口,其中包含了一个query方法,用于查询天气信息。 2. 实现WebService接口:接下来需要创建一个类来实现WebService接口。在引用\[2\]中的示例中,创建了一个名为WeatherServiceImpl的类,实现了WeatherService接口,并实现了query方法。 3. 发布WebService:将实现了WebService接口的类发布为一个WebService。这可以通过使用特定的框架或工具来完成。在引用\[2\]中的示例中,使用了WeatherServiceImplService类来发布WebService。 4. 编写WebService客户端:最后,需要编写一个客户端程序来调用WebService。在引用\[2\]中的示例中,创建了一个名为Main的客户端类,通过调用WeatherServiceImplService类的getPort方法获取WebService的端口,并调用query方法来查询天气信息。 总结起来,搭建WebService的过程包括创建WebService接口、实现WebService接口、发布WebService和编写WebService客户端。具体的实现方式可以根据具体的需求和使用的技术框架来选择。 #### 引用[.reference_title] - *1* [WebService创建及使用](https://blog.csdn.net/yunnGuitu/article/details/125611109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Java工作笔记-WebService使用JDK搭建WebService及调用](https://blog.csdn.net/qq78442761/article/details/107830620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值