在PHP框架中搭建简单的webservice——简单的WSDL的创建

1,WSDL模板

<?xml version ='1.0' encoding ='UTF-8' ?>   
<definitions name='自定义名称'   
  targetNamespace='目标命名空间(WSDL所在地址)'   
    <!--tns自定义目标空间,下面会用到-->  
  xmlns:tns='目标命名空间(WSDL所在地址)'  
  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="目标命名空间(WSDL所在地址)">   
    </xsd:schema>  
 </types>   
  
<!--  
<message> 元素可定义每个消息的部件,以及相关联的数据类型。  
请求-响应是最普通的操作类型,不过 WSDL 定义了四种类型:  
One-way 此操作可接受消息,但不会返回响应。  
Request-response    此操作可接受一个请求并会返回一个响应。(常用)  
Solicit-response    此操作可发送一个请求,并会等待一个响应。  
Notification    此操作可发送一条消息,但不会等待响应。  
-->    
<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='执行的操作名称(binding的type与其对应)'>   
  <operation name='执行操作的方法'>   
    <input message='tns:*Request'/>   
    <output message='tns:*response'/>   
  </operation>   
</portType>  
  
<!--<binding> 元素为每个端口定义消息格式和协议细节-->  
<binding name='Binding的名称,与service的port名称对应' type='指向用于Binding的端口(tns(前缀):PortType名称)'>   
<!--style:属性可取值 "rpc" 或 "document",ransport:属性定义了要使用的 SOAP 协议。在这个例子中我们使用 HTTP-->  
  <soap:binding style='rpc'   
    transport='http://schemas.xmlsoap.org/soap/http'/>   
    <!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->  
  <operation name='GetCallDetailRecords'>   
    <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='服务名称'>   
  <port name='Binding名称' binding='tns:Binding名称'>   
    <soap:address location='http://目标命名空间(WSDL所在地址)/service.php'/>   
  </port>   
</service>   
</definitions>  

2,修改WSDL模板,本示例创建了2个方法,即test跟add
目标命名空间(WSDL所在地址):填写service所在的URL,如http://127.0.0.1/test/index

<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions
        name="test"
        targetNamespace='http://127.0.0.1/test/index'
        xmlns:tns='http://127.0.0.1/test/index'
        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="https://www.zhedao.net/plugins/zd-test/index">
        </xsd:schema>
    </types>
    
    <message name='testRequest'>
        <part name="test" type="xsd:string"/>
    </message>
    
    <message name='testResponse'>
        <part name="value" type="xsd:string"/>
    </message>

    <message name='addRequest'>
        <part name="add" type="xsd:string"/>
    </message>
    
    <message name='addResponse'>
        <part name="value" type="xsd:string"/>
    </message>
    
    <portType name='testTest'>
        <operation name='test'>
            <input message='tns:testRequest'/>
            <output message='tns:testResponse'/>
        </operation>
    </portType>

    <portType name='addTest'>
        <operation name='add'>
            <input message='tns:addRequest'/>
            <output message='tns:addResponse'/>
        </operation>
    </portType>
 
    <binding name='testBinding' type='tns:testTest'>
        <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>

    <binding name='addTestBinding' type='tns:addTestt'>
        <soap:binding style='rpc'
                      transport='http://schemas.xmlsoap.org/soap/http'/>
        <operation name="addTest">
            <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='testService'>
        <port name='testBinding' binding='tns:testBinding'>
            <soap:address location='http://127.0.0.1/test/index'/>
        </port>
        <port name='addTestBinding' binding='tns:addTestBinding'>
            <soap:address location='http://127.0.0.1/test/index'/>
        </port>
    </service>
</definitions>

3,创建serveice

<?php

namespace Test\Controller;

use test\plugin\BaseController;

class Test extends BaseController
{
    public function index()
    {
        $server = new \SoapServer('http://127.0.0.1/plugins/wsdl.xml');
        $server->setClass('Test\Controller\ZdTest');
        $server->handle();
    }

    public function test($data)
    {
        return $data;
    }

    public function add($data)
    {
		$num = json_decode($data, true);
        return $num[0] + $num[1];
    }
}

4,创建client

<?php

namespace Test\Controller;

use test\plugin\BaseController;

class TestTest extends BaseController
{
    public function index($req)
    {
        $soap = new \SoapClient('http://127.0.0.1/plugins/wsdl.xml');

        $soap->http_encoding = 'UTF-8';
        $soap->xml_encoding = 'UTF-8';
        $soap->decode_utf8 = false;

        echo $soap->add(json_encode([1,5])) . '<br>';
        echo $soap->test('测试通过');
    }
}

5,输出结果如下

6
测试通过

6,注意事项

1:传输多个参数时,使用json或者xml格式传输;

2:例子的URL链接只是举例,实际测试时,请使用linux服务器,本地windows服务器可能出现无限504超时的问题;

3:wsdl.xml文件一旦加载通过,再修改此文件里面的内容,无法立即生效,可以通过清除缓存(未验证)和修改文件名生效;

7,吐槽
webservice究竟干什么用的,传输一些简单的数据用webApi不就行了,简单轻便开发效率也高

8,相关文章
基于PHP——简单的WSDL的创建(WSDL篇):
https://blog.csdn.net/rrr4578/article/details/24451943

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值