meng-tian/php-soap-interpreter

一个用于解析 SOAP 消息的 PHP 库。

2.0.0 2022-11-19 00:20 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:21:41 UTC


README

一个用于解析 SOAP 1.1SOAP 1.2 消息的 PHP 库。它可以在 WSDL 或非 WSDL 模式下使用。该实现基于 PHP 的 SoapClient

前提条件

PHP 7.1 --enablelibxml --enable-soap

安装

composer require meng-tian/php-soap-interpreter

使用方法

Interpreter 负责生成 SOAP 请求消息和转换 SOAP 响应消息。Interpreter 类的构造函数与 SoapClient 相同。第一个参数是 wsdl,第二个参数是一个包含 options 的数组。

需要注意的是,并非所有 SoapClient 支持的 options 都由 Interpreter 支持。支持的 Interpreter 选项包括:locationuristyleusesoap_versionencodingexceptionsclassmaptypemapcache_wsdlfeatures。有关这些选项的更详细说明,请参阅 SoapClient::SoapClient。不支持的选项与调试或 HTTP 传输有关,这些不是 Interpreter 的预期职责。

基本示例

在 WSDL 模式下生成 SOAP 请求消息
$interpreter = new Interpreter('http://www.webservicex.net/length.asmx?WSDL');
$request = $interpreter->request(
    'ChangeLengthUnit',
    [['LengthValue'=>'1', 'fromLengthUnit'=>'Inches', 'toLengthUnit'=>'Meters']]
);

print_r($request->getSoapMessage());

输出

<?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/">
<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>
转换 SOAP 响应消息
$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:
stdClass Object
(
    [ChangeLengthUnitResult] => 0.0254
)
*/

高级示例

在非 WSDL 模式下生成 SOAP 请求消息
// 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());

输出

<?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: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>
SOAP 输入头
$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());

输出

<?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>
SOAP 输出头

待办事项

类映射

待办事项

类型映射

待办事项

相关

许可证

本库在 MIT 许可证下发布。