viktorprogger/php-soap-interpreter

用于解析 SOAP 消息的 PHP 库。

1.0.2 2016-05-21 13:19 UTC

This package is auto-updated.

Last update: 2024-09-25 21:22:32 UTC


README

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

需求

PHP 5.4 --enablelibxml --enable-soap

安装

composer require meng-tian/php-soap-interpreter

用法

一个 Interpreter 实例能够生成 SOAP 请求消息并翻译 SOAP 响应消息。与 SoapClient 类似,Interpreter 类的构造函数也是一样的。第一个参数是 wsdl,第二个参数是 options 数组。

需要注意的是,并不是所有由 SoapClient 支持的 options 都被 Interpreter 支持。Interpreter 的责任是解析 SOAP 消息,而不支持选项与调试或 HTTP 传输相关。Interpreter 支持的 options 包括:locationuristyleusesoap_versionencodingexceptionsclassmaptypemapcache_wsdlfeature。有关这些选项的更详细解释,请参阅 SoapClient::SoapClient

示例

在 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>
在非 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/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
)
*/

相关

致谢

感谢免费 SOAP 网络服务提供商 http://www.webservicex.net。它们被用于测试此库。

许可证

此库在 MIT 许可下发布。