2011-09-26 22:42:21 +02:00
|
|
|
<?php
|
2011-10-03 21:03:46 +02:00
|
|
|
|
|
|
|
/*
|
2011-09-26 22:42:21 +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;
|
|
|
|
|
2011-12-11 21:20:35 +01:00
|
|
|
use BeSimple\SoapCommon\SoapKernel;
|
|
|
|
|
2011-09-26 22:42:21 +02: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
|
2011-09-26 22:42:21 +02:00
|
|
|
* allows caching of all remote referenced items.
|
|
|
|
*
|
2011-10-22 11:28:15 +02:00
|
|
|
* @author Andreas Schamberger <mail@andreass.net>
|
2011-09-26 22:42:21 +02:00
|
|
|
*/
|
|
|
|
class SoapClient extends \SoapClient
|
|
|
|
{
|
2011-11-02 12:48:51 +01:00
|
|
|
/**
|
|
|
|
* Soap version.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $soapVersion = SOAP_1_1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2011-09-26 22:42:21 +02:00
|
|
|
*/
|
|
|
|
private $lastRequestHeaders = '';
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last request.
|
|
|
|
*
|
|
|
|
* @var string
|
2011-09-26 22:42:21 +02:00
|
|
|
*/
|
|
|
|
private $lastRequest = '';
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last response headers.
|
|
|
|
*
|
|
|
|
* @var string
|
2011-09-26 22:42:21 +02:00
|
|
|
*/
|
|
|
|
private $lastResponseHeaders = '';
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Last response.
|
|
|
|
*
|
|
|
|
* @var string
|
2011-09-26 22:42:21 +02:00
|
|
|
*/
|
|
|
|
private $lastResponse = '';
|
|
|
|
|
2011-12-11 21:20:35 +01:00
|
|
|
/**
|
|
|
|
* Last response.
|
|
|
|
*
|
|
|
|
* @var \BeSimple\SoapCommon\SoapKernel
|
|
|
|
*/
|
|
|
|
protected $soapKernel = null;
|
|
|
|
|
2011-09-26 22:42:21 +02:00
|
|
|
/**
|
2011-11-02 12:48:51 +01:00
|
|
|
* Constructor.
|
2011-09-26 22:42:21 +02:00
|
|
|
*
|
2011-11-02 12:48:51 +01:00
|
|
|
* @param string $wsdl WSDL file
|
|
|
|
* @param array(string=>mixed) $options Options array
|
2011-09-26 22:42:21 +02:00
|
|
|
*/
|
2011-11-02 12:48:51 +01:00
|
|
|
public function __construct($wsdl, array $options = array())
|
2011-09-26 22:42:21 +02:00
|
|
|
{
|
2011-11-02 12:48:51 +01:00
|
|
|
// tracing enabled: store last request/response header and body
|
|
|
|
if (isset($options['trace']) && $options['trace'] === true) {
|
|
|
|
$this->tracingEnabled = true;
|
2011-09-26 22:42:21 +02:00
|
|
|
}
|
2011-11-02 12:48:51 +01:00
|
|
|
// store SOAP version
|
|
|
|
if (isset($options['soap_version'])) {
|
|
|
|
$this->soapVersion = $options['soap_version'];
|
2011-09-26 22:42:21 +02:00
|
|
|
}
|
2011-11-02 12:48:51 +01:00
|
|
|
$this->curl = new Curl($options);
|
|
|
|
$wsdlFile = $this->loadWsdl($wsdl, $options);
|
2011-12-11 21:20:35 +01:00
|
|
|
// TODO $wsdlHandler = new WsdlHandler($wsdlFile, $this->soapVersion);
|
|
|
|
$this->soapKernel = new SoapKernel();
|
2011-11-02 12:48:51 +01:00
|
|
|
// we want the exceptions option to be set
|
|
|
|
$options['exceptions'] = true;
|
2011-09-26 22:42:21 +02:00
|
|
|
// disable obsolete trace option for native SoapClient as we need to do our own tracing anyways
|
|
|
|
$options['trace'] = false;
|
2011-10-16 19:49:24 +02:00
|
|
|
// disable WSDL caching as we handle WSDL caching for remote URLs ourself
|
2011-09-26 22:42:21 +02:00
|
|
|
$options['cache_wsdl'] = WSDL_CACHE_NONE;
|
2011-11-02 12:48:51 +01:00
|
|
|
parent::__construct($wsdlFile, $options);
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
2011-09-26 22:42:21 +02:00
|
|
|
|
2011-11-02 12:48:51 +01:00
|
|
|
|
2011-09-26 22:42:21 +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-09-26 22:42:21 +02:00
|
|
|
*/
|
2011-12-11 21:20:35 +01:00
|
|
|
private function __doHttpRequest(SoapRequest $soapRequest)
|
2011-09-26 22:42:21 +02:00
|
|
|
{
|
2011-12-11 21:20:35 +01:00
|
|
|
// HTTP headers
|
|
|
|
$headers = array(
|
|
|
|
'Content-Type:' . $soapRequest->getContentType(),
|
|
|
|
'SOAPAction: "' . $soapRequest->getAction() . '"',
|
|
|
|
);
|
|
|
|
// execute HTTP request with cURL
|
|
|
|
$responseSuccessfull = $this->curl->exec($soapRequest->getLocation(),
|
|
|
|
$soapRequest->getContent(),
|
2011-12-17 16:05:25 +01:00
|
|
|
$headers);
|
2011-09-26 22:42:21 +02:00
|
|
|
// tracing enabled: store last request header and body
|
2011-11-02 12:48:51 +01:00
|
|
|
if ($this->tracingEnabled === true) {
|
2011-12-11 21:20:35 +01:00
|
|
|
$this->lastRequestHeaders = $this->curl->getRequestHeaders();
|
|
|
|
$this->lastRequest = $soapRequest->getContent();
|
2011-09-26 22:42:21 +02:00
|
|
|
}
|
|
|
|
// in case of an error while making the http request throw a soapFault
|
2011-10-02 12:23:59 +02:00
|
|
|
if ($responseSuccessfull === false) {
|
2011-09-26 22:42:21 +02:00
|
|
|
// get error message from curl
|
2011-11-02 12:48:51 +01:00
|
|
|
$faultstring = $this->curl->getErrorMessage();
|
2011-12-17 16:05:25 +01:00
|
|
|
throw new \SoapFault('HTTP', $faultstring);
|
2011-09-26 22:42:21 +02:00
|
|
|
}
|
|
|
|
// tracing enabled: store last response header and body
|
2011-11-02 12:48:51 +01:00
|
|
|
if ($this->tracingEnabled === true) {
|
|
|
|
$this->lastResponseHeaders = $this->curl->getResponseHeaders();
|
|
|
|
$this->lastResponse = $this->curl->getResponseBody();
|
2011-09-26 22:42:21 +02:00
|
|
|
}
|
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-09-26 22:42:21 +02:00
|
|
|
|
2011-12-17 16:05:25 +01:00
|
|
|
/**
|
2011-09-26 22:42:21 +02: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.
|
2011-09-26 22:42:21 +02:00
|
|
|
*
|
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
|
|
|
|
*
|
2011-09-26 22:42:21 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-12-11 21:20:35 +01:00
|
|
|
public function __doRequest($request, $location, $action, $version, $oneWay = 0)
|
2011-09-26 22:42:21 +02:00
|
|
|
{
|
2011-12-11 21:20:35 +01:00
|
|
|
// wrap request data in SoapRequest object
|
|
|
|
$soapRequest = SoapRequest::create($request, $location, $action, $version);
|
|
|
|
|
|
|
|
// do actual SOAP request
|
|
|
|
$soapResponse = $this->__doRequest2($soapRequest);
|
|
|
|
|
2011-09-26 22:42:21 +02:00
|
|
|
// 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
|
|
|
|
$soapRequest = $this->soapKernel->filterRequest($soapRequest);
|
|
|
|
|
|
|
|
// perform HTTP request with cURL
|
|
|
|
$soapResponse = $this->__doHttpRequest($soapRequest);
|
|
|
|
|
|
|
|
// run SoapKernel on SoapResponse
|
|
|
|
$soapResponse = $this->soapKernel->filterResponse($soapResponse);
|
|
|
|
|
|
|
|
return $soapResponse;
|
2011-09-26 22:42:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return \BeSimple\SoapCommon\SoapKernel
|
|
|
|
*/
|
|
|
|
public function getSoapKernel()
|
|
|
|
{
|
|
|
|
return $this->soapKernel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO finish
|
|
|
|
protected function isValidSoapResponse()
|
|
|
|
{
|
|
|
|
//check if we do have a proper soap status code (if not soapfault)
|
|
|
|
$responseStatusCode = $this->curl->getResponseStatusCode();
|
|
|
|
$response = $this->curl->getResponseBody();
|
|
|
|
if ($responseStatusCode >= 400) {
|
|
|
|
$isError = 0;
|
|
|
|
$response = trim($response);
|
|
|
|
if (strlen($response) == 0) {
|
|
|
|
$isError = 1;
|
|
|
|
} else {
|
|
|
|
$contentType = $this->curl->getResponseContentType();
|
|
|
|
if ($contentType != 'application/soap+xml'
|
|
|
|
&& $contentType != 'application/soap+xml') {
|
2011-12-17 16:05:25 +01:00
|
|
|
if (strncmp($response, "<?xml", 5)) {
|
2011-12-11 21:20:35 +01:00
|
|
|
$isError = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($isError == 1) {
|
|
|
|
throw new \SoapFault('HTTP', $this->curl->getResponseStatusMessage());
|
|
|
|
}
|
|
|
|
} elseif ($responseStatusCode != 200 && $responseStatusCode != 202) {
|
|
|
|
$dom = new \DOMDocument('1.0');
|
|
|
|
$dom->loadXML($response);
|
|
|
|
if ($dom->getElementsByTagNameNS($dom->documentElement->namespaceURI, 'Fault')->length == 0) {
|
|
|
|
throw new \SoapFault('HTTP', 'HTTP response status must be 200 or 202');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 22:42:21 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2011-11-02 12:48:51 +01:00
|
|
|
* @param string $wsdl WSDL file
|
|
|
|
* @param array(string=>mixed) $options Options array
|
2011-12-17 16:05:25 +01:00
|
|
|
*
|
2011-09-26 22:42:21 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-11-02 12:48:51 +01:00
|
|
|
private function loadWsdl($wsdl, array $options)
|
2011-09-26 22:42:21 +02:00
|
|
|
{
|
2011-11-20 18:13:42 +01:00
|
|
|
// option to resolve wsdl/xsd includes
|
|
|
|
$resolveRemoteIncludes = true;
|
|
|
|
if (isset($options['resolve_wsdl_remote_includes'])) {
|
|
|
|
$resolveRemoteIncludes = $options['resolve_wsdl_remote_includes'];
|
2011-11-02 12:48:51 +01:00
|
|
|
}
|
|
|
|
// option to enable cache
|
|
|
|
$wsdlCache = WSDL_CACHE_DISK;
|
2011-11-20 18:13:42 +01:00
|
|
|
if (isset($options['cache_wsdl'])) {
|
2011-11-02 12:48:51 +01:00
|
|
|
$wsdlCache = $options['cache_wsdl'];
|
|
|
|
}
|
2011-11-20 18:13:42 +01:00
|
|
|
$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-09-26 22:42:21 +02:00
|
|
|
}
|
2011-12-11 21:20:35 +01:00
|
|
|
|
2011-09-26 22:42:21 +02:00
|
|
|
return $cacheFileName;
|
|
|
|
}
|
|
|
|
}
|