首先要创建两个文件Test.php Test.wsdl
Test.php文件代码是处理具体业务的接口代码
Test.php代码:
?php
ini_set("soap.wsdl_cache_enabled", "0");
class Test
{
function getData($Content)//接口参数,可有可无,看具体需求
{
$res="<xml><status>1</status>
<msg>操作成功</msg></xml>";
return $res;
}
function addData($Content)//接口参数,可有可无,看具体需求
{
$res="<xml><status>1</status>
<msg>操作成功</msg></xml>";
return $res;
}
}
$service = new SoapServer('http://127.0.0.1/webservice/Test.wsdl', array('soap_version' => SOAP_1_2));
$service->setClass("Test"); //! 注册Service类的所有方法
$service->handle(); //! 处理请求*/
?>
Test.wsdl文件代码:
<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Test' targetNamespace='http://127.0.0.1/webservice'
xmlns:tns='http://127.0.0.1/webservice' 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://127.0.0.1/Test">
</xsd:schema>
</types>
<!--
<message> 元素可定义每个消息的部件,以及相关联的数据类型。
请求-响应是最普通的操作类型,不过 WSDL 定义了四种类型:
One-way 此操作可接受消息,但不会返回响应。
Request-response 此操作可接受一个请求并会返回一个响应。(常用)
Solicit-response 此操作可发送一个请求,并会等待一个响应。
Notification 此操作可发送一条消息,但不会等待响应。
-->
<message name='getDate'>
<part name="Content" type="xsd:string"/>
</message>
<message name='addDate'>
<part name="Content" type="xsd:string"/>
</message>
<message name='getDataResponse'>
<part name="getDataRespose" type="xsd:string"/>
</message>
<message name='addDataResponse'>
<part name="addDataRespose" type="xsd:string"/>
</message>
<!--
<portType> 元素是最重要的 WSDL 元素。它可描述一个 web service、可被执行的操作,以及相关的消息。
它告诉你去哪个WebService的连接点,扮演了一个控制者。
-->
<portType name='Test'>
<operation name='getData'>
<input message='tns:getDataRequest'/>
<output message='tns:getDataResponse'/>
</operation>
<operation name='addData'>
<input message='tns:addDataRequest'/>
<output message='tns:addDataResponse'/>
</operation>
</portType>
<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name='Test' type='Test'>
<!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP-->
<soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/>
<!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
<operation name='payInfor'>
<soap:operation soapAction='http://www.example.org/Test/getData'/>
<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='Debit'>
<port name='Debit' binding='tns:Test'>
<soap:address location='http://127.0.0.1/webservice/Test.php'/>
</port>
</service>
</definitions>
以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要
PHP进阶架构师>>>视频、面试文档免费获取docs.qq.com![v2-2686790c46d4e5db4d4b344c23a17517_ipico.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=76ab9fe5-492e-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-2686790c46d4e5db4d4b344c23a17517_ipico.jpg)
或 者关注咱们下面的知乎专栏
PHP大神进阶zhuanlan.zhihu.com![v2-98ed0acdcd86b9af33d979844466f249_ipico.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=76ab9fe5-492e-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-98ed0acdcd86b9af33d979844466f249_ipico.jpg)
来源:https://www.cnblogs.com/zhengyixin/p/12502540.html