BeSimpleSoap/src/BeSimple/SoapClient/SoapClient.php

364 lines
11 KiB
PHP
Raw Normal View History

<?php
2011-10-03 21:03:46 +02:00
/*
* This file is part of the BeSimpleSoapClient.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapClient;
2012-01-03 18:50:45 +01:00
use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\Converter\MtomTypeConverter;
use BeSimple\SoapCommon\Converter\SwaTypeConverter;
use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFactory;
2011-12-11 21:20:35 +01:00
/**
2011-10-02 12:23:59 +02:00
* Extended SoapClient that uses a a cURL wrapper for all underlying HTTP
* requests in order to use proper authentication for all requests. This also
* adds NTLM support. A custom WSDL downloader resolves remote xsd:includes and
* allows caching of all remote referenced items.
*
2011-10-22 11:28:15 +02:00
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapClient extends \SoapClient
{
protected $soapVersion;
/**
* Tracing enabled?
*
* @var boolean
*/
protected $tracingEnabled = false;
/**
* cURL instance.
*
* @var \BeSimple\SoapClient\Curl
*/
protected $curl = null;
2011-10-16 19:49:24 +02:00
/**
* Last request headers.
*
* @var string
*/
private $lastRequestHeaders = '';
2011-10-16 19:49:24 +02:00
/**
* Last request.
*
* @var string
*/
private $lastRequest = '';
2011-10-16 19:49:24 +02:00
/**
* Last response headers.
*
* @var string
*/
private $lastResponseHeaders = '';
2011-10-16 19:49:24 +02:00
/**
* Last response.
*
* @var string
*/
private $lastResponse = '';
2011-12-11 21:20:35 +01:00
/**
2012-01-15 11:42:21 +01:00
* Soap kernel.
2011-12-11 21:20:35 +01:00
*
2012-04-22 18:08:40 +02:00
* @var \BeSimple\SoapClient\SoapKernel
2011-12-11 21:20:35 +01:00
*/
protected $soapKernel = null;
/**
* Constructor.
*
* @param SoapClientOptions $soapClientOptions
* @param SoapOptions $soapOptions
*/
public function __construct(SoapClientOptions $soapClientOptions, SoapOptions $soapOptions)
{
$this->soapKernel = new SoapKernel();
$this->soapVersion = $soapOptions->getSoapVersion();
$this->tracingEnabled = $soapClientOptions->getTrace();
// @todo: refactor SoapClient: do not use $options as array
$options = $this->configureMime($soapOptions->toArray());
// @todo: refactor SoapClient: do not use $options as array
$this->curl = new Curl($soapClientOptions->toArray());
// @todo: refactor SoapClient: do not use $options as array
$wsdlFile = $this->loadWsdl($soapOptions->getWsdlFile(), $soapOptions->toArray());
parent::__construct($wsdlFile, $options);
2011-10-16 19:49:24 +02:00
}
/**
* Perform HTTP request with cURL.
*
2011-12-17 16:05:25 +01:00
* @param SoapRequest $soapRequest SoapRequest object
*
2011-12-11 21:20:35 +01:00
* @return SoapResponse
*/
2011-12-11 21:20:35 +01:00
private function __doHttpRequest(SoapRequest $soapRequest)
{
2011-12-11 21:20:35 +01:00
// HTTP headers
2013-08-30 22:07:44 +02:00
$soapVersion = $soapRequest->getVersion();
$soapAction = $soapRequest->getAction();
if (SOAP_1_1 == $soapVersion) {
$headers = array(
'Content-Type:' . $soapRequest->getContentType(),
'SOAPAction: "' . $soapAction . '"',
);
} else {
$headers = array(
'Content-Type:' . $soapRequest->getContentType() . '; action="' . $soapAction . '"',
);
}
$location = $soapRequest->getLocation();
$content = $soapRequest->getContent();
$headers = $this->filterRequestHeaders($soapRequest, $headers);
2014-07-01 15:47:46 +02:00
$options = $this->filterRequestOptions($soapRequest);
2011-12-11 21:20:35 +01:00
// execute HTTP request with cURL
$responseSuccessfull = $this->curl->exec(
$location,
$content,
2014-07-01 15:47:46 +02:00
$headers,
$options
);
// tracing enabled: store last request header and body
if ($this->tracingEnabled === true) {
2011-12-11 21:20:35 +01:00
$this->lastRequestHeaders = $this->curl->getRequestHeaders();
$this->lastRequest = $soapRequest->getContent();
}
// in case of an error while making the http request throw a soapFault
2011-10-02 12:23:59 +02:00
if ($responseSuccessfull === false) {
// get error message from curl
$faultstring = $this->curl->getErrorMessage();
2011-12-17 16:05:25 +01:00
throw new \SoapFault('HTTP', $faultstring);
}
// tracing enabled: store last response header and body
if ($this->tracingEnabled === true) {
$this->lastResponseHeaders = $this->curl->getResponseHeaders();
$this->lastResponse = $this->curl->getResponseBody();
}
2011-12-11 21:20:35 +01:00
// wrap response data in SoapResponse object
2011-12-17 16:05:25 +01:00
$soapResponse = SoapResponse::create(
$this->curl->getResponseBody(),
2011-12-11 21:20:35 +01:00
$soapRequest->getLocation(),
$soapRequest->getAction(),
$soapRequest->getVersion(),
2011-12-17 16:05:25 +01:00
$this->curl->getResponseContentType()
);
2011-12-11 21:20:35 +01:00
return $soapResponse;
2011-12-17 16:05:25 +01:00
}
2011-12-17 16:05:25 +01:00
/**
* Custom request method to be able to modify the SOAP messages.
2011-12-11 21:20:35 +01:00
* $oneWay parameter is not used at the moment.
*
* @todo: refactor SoapClient: refactoring starts from here
2011-12-17 16:05:25 +01:00
* @param string $request Request string
* @param string $location Location
* @param string $action SOAP action
* @param int $version SOAP version
* @param int $oneWay 0|1
*
* @return string
*/
2011-12-11 21:20:35 +01:00
public function __doRequest($request, $location, $action, $version, $oneWay = 0)
{
2011-12-11 21:20:35 +01:00
// wrap request data in SoapRequest object
$soapRequest = SoapRequestFactory::create($location, $action, $version, $request);
2011-12-11 21:20:35 +01:00
// do actual SOAP request
$soapResponse = $this->__doRequest2($soapRequest);
// return SOAP response to ext/soap
2011-12-11 21:20:35 +01:00
return $soapResponse->getContent();
}
/**
* Runs the currently registered request filters on the request, performs
* the HTTP request and runs the response filters.
*
2011-12-17 16:05:25 +01:00
* @param SoapRequest $soapRequest SOAP request object
*
2011-12-11 21:20:35 +01:00
* @return SoapResponse
*/
protected function __doRequest2(SoapRequest $soapRequest)
{
// run SoapKernel on SoapRequest
$this->soapKernel->filterRequest($soapRequest);
2011-12-11 21:20:35 +01:00
// perform HTTP request with cURL
$soapResponse = $this->__doHttpRequest($soapRequest);
// run SoapKernel on SoapResponse
$this->soapKernel->filterResponse($soapResponse);
2011-12-11 21:20:35 +01:00
return $soapResponse;
}
/**
* Filters HTTP headers which will be sent
*
* @param SoapRequest $soapRequest SOAP request object
* @param array $headers An array of HTTP headers
*
* @return array
*/
protected function filterRequestHeaders(SoapRequest $soapRequest, array $headers)
{
return $headers;
}
2014-07-01 15:47:46 +02:00
/**
* Adds additional cURL options for the request
*
* @param SoapRequest $soapRequest SOAP request object
*
* @return array
*/
protected function filterRequestOptions(SoapRequest $soapRequest)
{
return array();
}
/**
* Get last request HTTP headers.
*
* @return string
*/
public function __getLastRequestHeaders()
{
return $this->lastRequestHeaders;
}
/**
* Get last request HTTP body.
*
* @return string
*/
public function __getLastRequest()
{
return $this->lastRequest;
}
/**
* Get last response HTTP headers.
*
* @return string
*/
public function __getLastResponseHeaders()
{
return $this->lastResponseHeaders;
}
/**
* Get last response HTTP body.
*
* @return string
*/
public function __getLastResponse()
{
return $this->lastResponse;
}
2011-12-11 21:20:35 +01:00
/**
* Get SoapKernel instance.
*
2012-04-22 18:08:40 +02:00
* @return \BeSimple\SoapClient\SoapKernel
2011-12-11 21:20:35 +01:00
*/
public function getSoapKernel()
{
return $this->soapKernel;
}
private function configureMime(array $options)
{
2016-11-01 18:13:23 +01:00
// @todo: PBe: refactor same as SoapServer
if (Helper::ATTACHMENTS_TYPE_BASE64 !== $options['attachment_type']) {
// register mime filter in SoapKernel
$mimeFilter = new MimeFilter($options['attachment_type']);
$this->soapKernel->registerFilter($mimeFilter);
// configure type converter
if (Helper::ATTACHMENTS_TYPE_SWA === $options['attachment_type']) {
$converter = new SwaTypeConverter();
$converter->setKernel($this->soapKernel);
} elseif (Helper::ATTACHMENTS_TYPE_MTOM === $options['attachment_type']) {
2012-01-29 14:38:12 +01:00
$xmlMimeFilter = new XmlMimeFilter($options['attachment_type']);
$this->soapKernel->registerFilter($xmlMimeFilter);
$converter = new MtomTypeConverter();
$converter->setKernel($this->soapKernel);
}
// configure typemap
if (!isset($options['typemap'])) {
$options['typemap'] = array();
}
$options['typemap'][] = array(
'type_name' => $converter->getTypeName(),
'type_ns' => $converter->getTypeNamespace(),
'from_xml' => function($input) use ($converter) {
return $converter->convertXmlToPhp($input);
},
'to_xml' => function($input) use ($converter) {
return $converter->convertPhpToXml($input);
},
);
}
return $options;
}
/**
* Downloads WSDL files with cURL. Uses all SoapClient options for
* authentication. Uses the WSDL_CACHE_* constants and the 'soap.wsdl_*'
* ini settings. Does only file caching as SoapClient only supports a file
* name parameter.
*
* @param string $wsdl WSDL file
* @param array(string=>mixed) $options Options array
2011-12-17 16:05:25 +01:00
*
* @return string
*/
protected function loadWsdl($wsdl, array $options)
{
// option to resolve wsdl/xsd includes
$resolveRemoteIncludes = true;
if (isset($options['resolve_wsdl_remote_includes'])) {
$resolveRemoteIncludes = $options['resolve_wsdl_remote_includes'];
}
// option to enable cache
$wsdlCache = WSDL_CACHE_DISK;
if (isset($options['cache_wsdl'])) {
$wsdlCache = $options['cache_wsdl'];
}
$wsdlDownloader = new WsdlDownloader($this->curl, $resolveRemoteIncludes, $wsdlCache);
2011-10-02 12:23:59 +02:00
try {
$cacheFileName = $wsdlDownloader->download($wsdl);
} catch (\RuntimeException $e) {
throw new \SoapFault('WSDL', "SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl . "' : failed to load external entity \"" . $wsdl . "\"");
}
2011-12-11 21:20:35 +01:00
return $cacheFileName;
}
}