在thinkphp5 中添加soap服务和客户端

  由于公司业务需要需要开发一个基于WSDL标准的soap服务端,由于公司的后台api使用的是thinkphp5.1,所以需要在thinkphp5.1中添加一个soap服务端

  第三方提供的开发文档推荐的是nusoap 但是我查了这个php 库是一个好几年前的库了,所以我就想到了使用php_soap扩展去开发这块

wsdl文档test.wsdl内容如下

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:s="http://www.w3.org/2001/XMLSchema"
             xmlns:s0="myServer"
             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
             xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
             xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
             targetNamespace="myServer"
             xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <s:schema elementFormDefault="qualified" targetNamespace="myServer">
      <s:element name="reverse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="request" type="s0:ReversalRequest" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ReversalRequest">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="language" type="s:string" />
        </s:sequence>
      </s:complexType>
      <s:element name="reverseResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="reverseResult" type="s0:ReversalResponse" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ReversalResponse">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="status" type="s0:Status" />
        </s:sequence>
      </s:complexType>
      <s:complexType name="Status">
        <s:sequence>
          <s:element minOccurs="1" maxOccurs="1" name="isError" type="s:boolean" />
          <s:element minOccurs="0" maxOccurs="1" name="errorCode" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="statusDescription" type="s:string" />
        </s:sequence>
      </s:complexType>
    </s:schema>
  </types>
  <message name="reverseSoapIn">
    <part name="parameters" element="s0:reverse" />
  </message>
  <message name="reverseSoapOut">
    <part name="parameters" element="s0:reverseResponse" />
  </message>
  <portType name="BillPaymentServiceSoap">
    <operation name="reverse">
      <input message="s0:reverseSoapIn" />
      <output message="s0:reverseSoapOut" />
    </operation>
  </portType>
  <binding name="BillPaymentServiceSoap" type="s0:BillPaymentServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    <operation name="reverse">
      <soap:operation soapAction="myServer#Payment#reverse" style="document" />
      <input>
        <soap:body use="literal" />
      </input>
      <output>
        <soap:body use="literal" />
      </output>
    </operation>
  </binding>
  <service name="BillPaymentService">
    <port name="BillPaymentServiceSoap" binding="s0:BillPaymentServiceSoap">
      <soap:address location="https://localhost/Test/server" />
    </port>
  </service>
</definitions>

 

根据test.wsdl文档规定的参数 提供的php服务如下

class Test extends Controller
{
    public function server(){
        $server = new \SoapServer(APP_PATH.'Test.wsdl',array('cache_wsdl' => WSDL_CACHE_NONE));
        Log::record("=====================server==========".date('Y-m-d H:i:s'),"info");
        $class = 'app\controller\Payment';
        $server->setClass($class);
        $server->handle();
        $soapXml = ob_get_contents();
        Log::record($soapXml,'info');
        ob_end_clean();
        $soapXml = trim($soapXml);
        //去掉特殊字符 不然客户端可能解析不了
        $soapXml = preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $soapXml);
        $length = strlen($soapXml);
        header("Content-Length: ".$length);
        echo $soapXml;
    }
}

 

需要在controller里面定义一个Payment 类

class Payment extends Controller
{
    public function reverse($inqueryRequest)
    {
        Log::record("=================================");
        Log::record($inqueryRequest);
        $inqueryRequest = json_decode(json_encode($inqueryRequest), true);
        Log::record($inqueryRequest);
        $status["isError"] = True;
        $status["errorCode"] = "ff";
        $status["statusDescription"] = "sss";
        $ReversalResponse['reverseResult']['status'] = $status;
        return $ReversalResponse;
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值