由于公司业务需要需要开发一个基于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;
}
}
ThinkPHP5.1 SOAP服务端实现
2449





