viktorprogger/soap-http-binding

一个将 SOAP 消息绑定到 PSR-7 HTTP 消息的 PHP 库。

0.2.3 2016-05-21 14:04 UTC

This package is auto-updated.

Last update: 2024-09-25 21:12:16 UTC


README

此库将 SOAP 1.1SOAP 1.2 消息绑定到 PSR-7 HTTP 消息。

要求

PHP 5.4

安装

composer require meng-tian/soap-http-binding

用法

HttpBinding::request 将 SOAP 请求消息嵌入 PSR-7 HTTP 请求。

use Meng\Soap\HttpBinding\HttpBinding;
use Meng\Soap\HttpBinding\RequestBuilder;
use Meng\Soap\Interpreter;

$interpreter = new Interpreter('http://www.webservicex.net/airport.asmx?WSDL');
$builder = new RequestBuilder();
$httpBinding = new HttpBinding($interpreter, $builder);

$request = $httpBinding->request('GetAirportInformationByCountry', [['country' => 'United Kingdom']]);
echo \Zend\Diactoros\Request\Serializer::toString($request);

输出

POST /airport.asmx HTTP/1.1
Content-Length: 322
SOAPAction: http://www.webserviceX.NET/GetAirportInformationByCountry
Content-Type: text/xml; charset="utf-8"
Host: www.webservicex.net

<?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:GetAirportInformationByCountry><ns1:country>United Kingdom</ns1:country></ns1:GetAirportInformationByCountry></SOAP-ENV:Body></SOAP-ENV:Envelope>

HttpBinding::response 从 PSR-7 HTTP 响应中检索 SOAP 响应消息

use Meng\Soap\HttpBinding\HttpBinding;
use Meng\Soap\HttpBinding\RequestBuilder;
use Meng\Soap\Interpreter;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;

$response = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAirportInformationByCountryResponse xmlns="http://www.webserviceX.NET">
      <GetAirportInformationByCountryResult>some result</GetAirportInformationByCountryResult>
    </GetAirportInformationByCountryResponse>
  </soap:Body>
</soap:Envelope>
EOD;

$stream = new Stream('php://memory', 'r+');
$stream->write($response);
$stream->rewind();
$response = new Response($stream, 200, ['Content-Type' => 'text/xml; charset=utf-8']);

$interpreter = new Interpreter('http://www.webservicex.net/airport.asmx?WSDL');
$builder = new RequestBuilder();
$httpBinding = new HttpBinding($interpreter, $builder);
$response = $httpBinding->response($response, 'GetAirportInformationByCountry');

print_r($response);

输出

stdClass Object
(
    [GetAirportInformationByCountryResult] => some result
)

此库还通过 RequestBuilder 类支持 SOAP 1.2 HTTP GET 绑定

use Meng\Soap\HttpBinding\RequestBuilder;

$builder = new RequestBuilder();
$request = $builder->isSOAP12()
    ->setEndpoint('http://www.endpoint.com')
    ->setHttpMethod('GET')
    ->getSoapHttpRequest();
echo \Zend\Diactoros\Request\Serializer::toString($request);

输出

GET / HTTP/1.1
Accept: application/soap+xml
Host: www.endpoint.com

许可证

此库在 MIT 许可下发布。