PHP SOAP Interpreter
A PHP library for interpreting SOAP 1.1
and SOAP 1.2
messages. It can be used in WSDL or non-WSDL mode. The implementation is built on the top of PHP's SoapClient., (*1)
Prerequisite
PHP 7.1 --enablelibxml --enable-soap, (*2)
Install
composer require meng-tian/php-soap-interpreter
Usage
An Interpreter
is responsible for generating SOAP request messages and translating SOAP response messages. The constructor of Interpreter
class is the same as SoapClient
. The first parameter is wsdl
, the second parameter is an array of options
., (*3)
It should be noted that not all options
supported by SoapClient
are supported by Interpreter
. The supported options
of Interpreter
are: location
, uri
, style
, use
, soap_version
, encoding
, exceptions
, classmap
, typemap
, cache_wsdl
and features
. More detailed explanations of those options can be found in SoapClient::SoapClient. The unsupported options are related to debugging or HTTP transport, which are not the intended responsibility of Interpreter
., (*4)
Basic Examples
Generate SOAP request message in WSDL mode
$interpreter = new Interpreter('http://www.webservicex.net/length.asmx?WSDL');
$request = $interpreter->request(
'ChangeLengthUnit',
[['LengthValue'=>'1', 'fromLengthUnit'=>'Inches', 'toLengthUnit'=>'Meters']]
);
print_r($request->getSoapMessage());
Output:, (*5)
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webserviceX.NET/">
<SOAP-ENV:Body><ns1:ChangeLengthUnit><ns1:LengthValue>1</ns1:LengthValue><ns1:fromLengthUnit>Inches</ns1:fromLengthUnit><ns1:toLengthUnit>Meters</ns1:toLengthUnit></ns1:ChangeLengthUnit></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Translate SOAP response message
$interpreter = new Interpreter('http://www.webservicex.net/length.asmx?WSDL');
$response = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ChangeLengthUnitResponse xmlns="http://www.webserviceX.NET/">
<ChangeLengthUnitResult>0.025400000000000002</ChangeLengthUnitResult>
</ChangeLengthUnitResponse>
</soap:Body>
</soap:Envelope>
EOD;
$response = $interpreter->response($response, 'ChangeLengthUnit');
print_r($response);
Output:, (*6)
/*
Output:
stdClass Object
(
[ChangeLengthUnitResult] => 0.0254
)
*/
Advanced Examples
Generate SOAP request message in non-WSDL mode
// In non-WSDL mode, location and uri must be provided as they are required by SoapClient.
$interpreter = new Interpreter(null, ['location'=>'http://www.webservicex.net/length.asmx', 'uri'=>'http://www.webserviceX.NET/']);
$request = $interpreter->request(
'ChangeLengthUnit',
[
new SoapParam('1', 'ns1:LengthValue'),
new SoapParam('Inches', 'ns1:fromLengthUnit'),
new SoapParam('Meters', 'ns1:toLengthUnit')
],
['soapaction'=>'http://www.webserviceX.NET/ChangeLengthUnit']
);
print_r($request->getSoapMessage());
Output:, (*7)
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webserviceX.NET/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><ns1:ChangeLengthUnit><ns1:LengthValue xsi:type="xsd:string">1</ns1:LengthValue><ns1:fromLengthUnit xsi:type="xsd:string">Inches</ns1:fromLengthUnit><ns1:toLengthUnit xsi:type="xsd:string">Meters</ns1:toLengthUnit></ns1:ChangeLengthUnit></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
$interpreter = new Interpreter('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL');
$request = $interpreter->request('ConversionRate', [['FromCurrency' => 'AFA', 'ToCurrency' => 'ALL']], null, [new SoapHeader('www.namespace.com', 'test_header', 'header_data')]);
print_r($request->getSoapMessage());
Output:, (*8)
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webserviceX.NET/" xmlns:ns2="www.namespace.com">
<SOAP-ENV:Header><ns2:test_header>header_data</ns2:test_header></SOAP-ENV:Header>
<SOAP-ENV:Body><ns1:ConversionRate><ns1:FromCurrency>AFA</ns1:FromCurrency><ns1:ToCurrency>ALL</ns1:ToCurrency></ns1:ConversionRate></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
TODO, (*9)
Class map
TODO, (*10)
Type map
TODO, (*11)
Relevant
License
This library is released under MIT license., (*12)