Curl instance is now given as parameter to WsdlDownloader + some
simplifications in client
This commit is contained in:
parent
cd0702c449
commit
f168e8566a
|
@ -22,6 +22,27 @@ namespace BeSimple\SoapClient;
|
||||||
*/
|
*/
|
||||||
class SoapClient extends \SoapClient
|
class SoapClient extends \SoapClient
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Last request headers.
|
* Last request headers.
|
||||||
*
|
*
|
||||||
|
@ -51,63 +72,33 @@ class SoapClient extends \SoapClient
|
||||||
private $lastResponse = '';
|
private $lastResponse = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy of the parent class' options array
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @var array(string=>mixed)
|
* @param string $wsdl WSDL file
|
||||||
|
* @param array(string=>mixed) $options Options array
|
||||||
*/
|
*/
|
||||||
protected $options = array();
|
public function __construct($wsdl, array $options = array())
|
||||||
|
|
||||||
/**
|
|
||||||
* Path to WSDL (cache) file.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $wsdlFile = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extended constructor that saves the options as the parent class'
|
|
||||||
* property is private.
|
|
||||||
*
|
|
||||||
* @param string $wsdl
|
|
||||||
* @param array(string=>mixed) $options
|
|
||||||
*/
|
|
||||||
public function __construct($wsdl, array $options = array(), TypeConverterCollection $converters = null)
|
|
||||||
{
|
{
|
||||||
|
// tracing enabled: store last request/response header and body
|
||||||
|
if (isset($options['trace']) && $options['trace'] === true) {
|
||||||
|
$this->tracingEnabled = true;
|
||||||
|
}
|
||||||
|
// store SOAP version
|
||||||
|
if (isset($options['soap_version'])) {
|
||||||
|
$this->soapVersion = $options['soap_version'];
|
||||||
|
}
|
||||||
|
$this->curl = new Curl($options);
|
||||||
|
$wsdlFile = $this->loadWsdl($wsdl, $options);
|
||||||
// we want the exceptions option to be set
|
// we want the exceptions option to be set
|
||||||
$options['exceptions'] = true;
|
$options['exceptions'] = true;
|
||||||
// we want to make sure we have the soap version to rely on it later
|
|
||||||
if (!isset($options['soap_version'])) {
|
|
||||||
$options['soap_version'] = SOAP_1_1;
|
|
||||||
}
|
|
||||||
// we want to make sure we have the features option
|
|
||||||
if (!isset($options['features'])) {
|
|
||||||
$options['features'] = 0;
|
|
||||||
}
|
|
||||||
// set default option to resolve xsd includes
|
|
||||||
if (!isset($options['resolve_xsd_includes'])) {
|
|
||||||
$options['resolve_xsd_includes'] = true;
|
|
||||||
}
|
|
||||||
// add type converters from TypeConverterCollection
|
|
||||||
if (!is_null($converters)) {
|
|
||||||
$convertersTypemap = $converters->getTypemap();
|
|
||||||
if (isset($options['typemap'])) {
|
|
||||||
$options['typemap'] = array_merge($options['typemap'], $convertersTypemap);
|
|
||||||
} else {
|
|
||||||
$options['typemap'] = $convertersTypemap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// store local copy as ext/soap's property is private
|
|
||||||
$this->options = $options;
|
|
||||||
// disable obsolete trace option for native SoapClient as we need to do our own tracing anyways
|
// disable obsolete trace option for native SoapClient as we need to do our own tracing anyways
|
||||||
$options['trace'] = false;
|
$options['trace'] = false;
|
||||||
// disable WSDL caching as we handle WSDL caching for remote URLs ourself
|
// disable WSDL caching as we handle WSDL caching for remote URLs ourself
|
||||||
$options['cache_wsdl'] = WSDL_CACHE_NONE;
|
$options['cache_wsdl'] = WSDL_CACHE_NONE;
|
||||||
// load WSDL and run parent constructor
|
parent::__construct($wsdlFile, $options);
|
||||||
// can't be loaded later as we need it already in the parent constructor
|
|
||||||
$this->wsdlFile = $this->loadWsdl($wsdl);
|
|
||||||
parent::__construct($this->wsdlFile, $options);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform HTTP request with cURL.
|
* Perform HTTP request with cURL.
|
||||||
*
|
*
|
||||||
|
@ -120,7 +111,7 @@ class SoapClient extends \SoapClient
|
||||||
{
|
{
|
||||||
// $request is if unmodified from SoapClient not a php string type!
|
// $request is if unmodified from SoapClient not a php string type!
|
||||||
$request = (string)$request;
|
$request = (string)$request;
|
||||||
if ($this->options['soap_version'] == SOAP_1_2) {
|
if ($this->soapVersion == SOAP_1_2) {
|
||||||
$headers = array(
|
$headers = array(
|
||||||
'Content-Type: application/soap+xml; charset=utf-8',
|
'Content-Type: application/soap+xml; charset=utf-8',
|
||||||
);
|
);
|
||||||
|
@ -131,39 +122,35 @@ class SoapClient extends \SoapClient
|
||||||
}
|
}
|
||||||
// add SOAPAction header
|
// add SOAPAction header
|
||||||
$headers[] = 'SOAPAction: "' . $action . '"';
|
$headers[] = 'SOAPAction: "' . $action . '"';
|
||||||
// new curl object for request
|
|
||||||
$curl = new Curl($this->options);
|
|
||||||
// execute request
|
// execute request
|
||||||
$responseSuccessfull = $curl->exec($location, $request, $headers);
|
$responseSuccessfull = $this->curl->exec($location, $request, $headers);
|
||||||
// tracing enabled: store last request header and body
|
// tracing enabled: store last request header and body
|
||||||
if (isset($this->options['trace']) && $this->options['trace'] === true) {
|
if ($this->tracingEnabled === true) {
|
||||||
$this->lastRequestHeaders = $curl->getRequestHeaders();
|
$this->lastRequestHeaders = $curl->getRequestHeaders();
|
||||||
$this->lastRequest = $request;
|
$this->lastRequest = $request;
|
||||||
}
|
}
|
||||||
// in case of an error while making the http request throw a soapFault
|
// in case of an error while making the http request throw a soapFault
|
||||||
if ($responseSuccessfull === false) {
|
if ($responseSuccessfull === false) {
|
||||||
// get error message from curl
|
// get error message from curl
|
||||||
$faultstring = $curl->getErrorMessage();
|
$faultstring = $this->curl->getErrorMessage();
|
||||||
// destruct curl object
|
|
||||||
unset($curl);
|
|
||||||
throw new \SoapFault('HTTP', $faultstring);
|
throw new \SoapFault('HTTP', $faultstring);
|
||||||
}
|
}
|
||||||
// tracing enabled: store last response header and body
|
// tracing enabled: store last response header and body
|
||||||
if (isset($this->options['trace']) && $this->options['trace'] === true) {
|
if ($this->tracingEnabled === true) {
|
||||||
$this->lastResponseHeaders = $curl->getResponseHeaders();
|
$this->lastResponseHeaders = $this->curl->getResponseHeaders();
|
||||||
$this->lastResponse = $curl->getResponseBody();
|
$this->lastResponse = $this->curl->getResponseBody();
|
||||||
}
|
}
|
||||||
$response = $curl->getResponseBody();
|
$response = $this->curl->getResponseBody();
|
||||||
// check if we do have a proper soap status code (if not soapfault)
|
// check if we do have a proper soap status code (if not soapfault)
|
||||||
// // TODO
|
// // TODO
|
||||||
// $responseStatusCode = $curl->getResponseStatusCode();
|
// $responseStatusCode = $this->curl->getResponseStatusCode();
|
||||||
// if ($responseStatusCode >= 400) {
|
// if ($responseStatusCode >= 400) {
|
||||||
// $isError = 0;
|
// $isError = 0;
|
||||||
// $response = trim($response);
|
// $response = trim($response);
|
||||||
// if (strlen($response) == 0) {
|
// if (strlen($response) == 0) {
|
||||||
// $isError = 1;
|
// $isError = 1;
|
||||||
// } else {
|
// } else {
|
||||||
// $contentType = $curl->getResponseContentType();
|
// $contentType = $this->curl->getResponseContentType();
|
||||||
// if ($contentType != 'application/soap+xml'
|
// if ($contentType != 'application/soap+xml'
|
||||||
// && $contentType != 'application/soap+xml') {
|
// && $contentType != 'application/soap+xml') {
|
||||||
// if (strncmp($response , "<?xml", 5)) {
|
// if (strncmp($response , "<?xml", 5)) {
|
||||||
|
@ -172,7 +159,7 @@ class SoapClient extends \SoapClient
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// if ($isError == 1) {
|
// if ($isError == 1) {
|
||||||
// throw new \SoapFault('HTTP', $curl->getResponseStatusMessage());
|
// throw new \SoapFault('HTTP', $this->curl->getResponseStatusMessage());
|
||||||
// }
|
// }
|
||||||
// } elseif ($responseStatusCode != 200 && $responseStatusCode != 202) {
|
// } elseif ($responseStatusCode != 200 && $responseStatusCode != 202) {
|
||||||
// $dom = new \DOMDocument('1.0');
|
// $dom = new \DOMDocument('1.0');
|
||||||
|
@ -181,8 +168,6 @@ class SoapClient extends \SoapClient
|
||||||
// throw new \SoapFault('HTTP', 'HTTP response status must be 200 or 202');
|
// throw new \SoapFault('HTTP', 'HTTP response status must be 200 or 202');
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// destruct curl object
|
|
||||||
unset($curl);
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,12 +235,25 @@ class SoapClient extends \SoapClient
|
||||||
* ini settings. Does only file caching as SoapClient only supports a file
|
* ini settings. Does only file caching as SoapClient only supports a file
|
||||||
* name parameter.
|
* name parameter.
|
||||||
*
|
*
|
||||||
* @param string $wsdl
|
* @param string $wsdl WSDL file
|
||||||
|
* @param array(string=>mixed) $options Options array
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function loadWsdl($wsdl)
|
private function loadWsdl($wsdl, array $options)
|
||||||
{
|
{
|
||||||
$wsdlDownloader = new WsdlDownloader($this->options);
|
// option to resolve xsd includes
|
||||||
|
$resolveXsdIncludes = true;
|
||||||
|
if (isset($options['resolve_xsd_includes']))
|
||||||
|
{
|
||||||
|
$resolveXsdIncludes = $options['resolve_xsd_includes'];
|
||||||
|
}
|
||||||
|
// option to enable cache
|
||||||
|
$wsdlCache = WSDL_CACHE_DISK;
|
||||||
|
if (isset($options['cache_wsdl']))
|
||||||
|
{
|
||||||
|
$wsdlCache = $options['cache_wsdl'];
|
||||||
|
}
|
||||||
|
$wsdlDownloader = new WsdlDownloader($this->curl, $resolveXsdIncludes, $wsdlCache);
|
||||||
try {
|
try {
|
||||||
$cacheFileName = $wsdlDownloader->download($wsdl);
|
$cacheFileName = $wsdlDownloader->download($wsdl);
|
||||||
} catch (\RuntimeException $e) {
|
} catch (\RuntimeException $e) {
|
||||||
|
|
|
@ -12,11 +12,14 @@
|
||||||
|
|
||||||
namespace BeSimple\SoapClient;
|
namespace BeSimple\SoapClient;
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
//use BeSimple\SoapCommon\Helper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads WSDL files with cURL. Uses all SoapClient options for
|
* Downloads WSDL files with cURL. Uses the WSDL_CACHE_* constants and the
|
||||||
* authentication. Uses the WSDL_CACHE_* constants and the 'soap.wsdl_*'
|
* 'soap.wsdl_*' ini settings. Does only file caching as SoapClient only
|
||||||
* ini settings. Does only file caching as SoapClient only supports a file
|
* supports a file name parameter. The class also resolves remote XML schema
|
||||||
* name parameter. The class also resolves remote XML schema includes.
|
* includes.
|
||||||
*
|
*
|
||||||
* @author Andreas Schamberger <mail@andreass.net>
|
* @author Andreas Schamberger <mail@andreass.net>
|
||||||
*/
|
*/
|
||||||
|
@ -44,24 +47,34 @@ class WsdlDownloader
|
||||||
private $cacheTtl;
|
private $cacheTtl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Options array
|
* cURL instance for downloads.
|
||||||
*
|
*
|
||||||
* @var array(string=>mixed)
|
* @var unknown_type
|
||||||
*/
|
*/
|
||||||
private $options = array();
|
private $curl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve XSD includes.
|
||||||
|
*
|
||||||
|
* @var boolean
|
||||||
|
*/
|
||||||
|
protected $resolveXsdIncludes = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param array $options
|
* @param \BeSimple\SoapClient\Curl $curl Curl instance
|
||||||
|
* @param boolean $resolveXsdIncludes XSD include enabled?
|
||||||
|
* @param boolean $cacheWsdl Cache constant
|
||||||
*/
|
*/
|
||||||
public function __construct(array $options = array())
|
public function __construct(Curl $curl, $resolveXsdIncludes = true, $cacheWsdl = WSDL_CACHE_DISK)
|
||||||
{
|
{
|
||||||
|
$this->curl = $curl;
|
||||||
|
$this->resolveXsdIncludes = $resolveXsdIncludes;
|
||||||
// get current WSDL caching config
|
// get current WSDL caching config
|
||||||
$this->cacheEnabled = (bool)ini_get('soap.wsdl_cache_enabled');
|
$this->cacheEnabled = (bool)ini_get('soap.wsdl_cache_enabled');
|
||||||
if ($this->cacheEnabled === true
|
if ($this->cacheEnabled === true
|
||||||
&& isset($options['cache_wsdl'])
|
&& $cacheWsdl === WSDL_CACHE_NONE) {
|
||||||
&& $options['cache_wsdl'] === WSDL_CACHE_NONE) {
|
|
||||||
$this->cacheEnabled = false;
|
$this->cacheEnabled = false;
|
||||||
}
|
}
|
||||||
$this->cacheDir = ini_get('soap.wsdl_cache_dir');
|
$this->cacheDir = ini_get('soap.wsdl_cache_dir');
|
||||||
|
@ -70,10 +83,6 @@ class WsdlDownloader
|
||||||
}
|
}
|
||||||
$this->cacheDir = rtrim($this->cacheDir, '/\\');
|
$this->cacheDir = rtrim($this->cacheDir, '/\\');
|
||||||
$this->cacheTtl = ini_get('soap.wsdl_cache_ttl');
|
$this->cacheTtl = ini_get('soap.wsdl_cache_ttl');
|
||||||
$this->options = $options;
|
|
||||||
if (!isset($this->options['resolve_xsd_includes'])) {
|
|
||||||
$this->options['resolve_xsd_includes'] = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,20 +96,18 @@ class WsdlDownloader
|
||||||
// download and cache remote WSDL files or local ones where we want to
|
// download and cache remote WSDL files or local ones where we want to
|
||||||
// resolve remote XSD includes
|
// resolve remote XSD includes
|
||||||
$isRemoteFile = $this->isRemoteFile($wsdl);
|
$isRemoteFile = $this->isRemoteFile($wsdl);
|
||||||
if ($isRemoteFile === true || $this->options['resolve_xsd_includes'] === true) {
|
if ($isRemoteFile === true || $this->resolveXsdIncludes === true) {
|
||||||
$cacheFile = $this->cacheDir . DIRECTORY_SEPARATOR . 'wsdl_' . md5($wsdl) . '.cache';
|
$cacheFile = $this->cacheDir . DIRECTORY_SEPARATOR . 'wsdl_' . md5($wsdl) . '.cache';
|
||||||
if ($this->cacheEnabled === false
|
if ($this->cacheEnabled === false
|
||||||
|| !file_exists($cacheFile)
|
|| !file_exists($cacheFile)
|
||||||
|| (filemtime($cacheFile) + $this->cacheTtl) < time()) {
|
|| (filemtime($cacheFile) + $this->cacheTtl) < time()) {
|
||||||
if ($isRemoteFile === true) {
|
if ($isRemoteFile === true) {
|
||||||
// new curl object for request
|
|
||||||
$curl = new Curl($this->options);
|
|
||||||
// execute request
|
// execute request
|
||||||
$responseSuccessfull = $curl->exec($wsdl);
|
$responseSuccessfull = $this->curl->exec($wsdl);
|
||||||
// get content
|
// get content
|
||||||
if ($responseSuccessfull === true) {
|
if ($responseSuccessfull === true) {
|
||||||
$response = $curl->getResponseBody();
|
$response = $this->curl->getResponseBody();
|
||||||
if ($this->options['resolve_xsd_includes'] === true) {
|
if ($this->resolveXsdIncludes === true) {
|
||||||
$this->resolveXsdIncludes($response, $cacheFile, $wsdl);
|
$this->resolveXsdIncludes($response, $cacheFile, $wsdl);
|
||||||
} else {
|
} else {
|
||||||
file_put_contents($cacheFile, $response);
|
file_put_contents($cacheFile, $response);
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
namespace BeSimple\SoapClient;
|
namespace BeSimple\SoapClient;
|
||||||
|
|
||||||
use BeSimple\SoapClient\WsdlDownloader;
|
use BeSimple\SoapClient\WsdlDownloader;
|
||||||
|
use BeSimple\SoapClient\Curl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andreas Schamberger <mail@andreass.net>
|
* @author Andreas Schamberger <mail@andreass.net>
|
||||||
|
@ -50,10 +51,8 @@ class WsdlDownloaderTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->startPhpWebserver();
|
$this->startPhpWebserver();
|
||||||
|
|
||||||
$options = array(
|
$curl = new Curl();
|
||||||
'resolve_xsd_includes' => true,
|
$wd = new WsdlDownloader($curl);
|
||||||
);
|
|
||||||
$wd = new WsdlDownloader($options);
|
|
||||||
|
|
||||||
$cacheDir = ini_get('soap.wsdl_cache_dir');
|
$cacheDir = ini_get('soap.wsdl_cache_dir');
|
||||||
if (!is_dir($cacheDir)) {
|
if (!is_dir($cacheDir)) {
|
||||||
|
@ -92,7 +91,8 @@ class WsdlDownloaderTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
public function testIsRemoteFile()
|
public function testIsRemoteFile()
|
||||||
{
|
{
|
||||||
$wd = new WsdlDownloader();
|
$curl = new Curl();
|
||||||
|
$wd = new WsdlDownloader($curl);
|
||||||
|
|
||||||
$class = new \ReflectionClass($wd);
|
$class = new \ReflectionClass($wd);
|
||||||
$method = $class->getMethod('isRemoteFile');
|
$method = $class->getMethod('isRemoteFile');
|
||||||
|
@ -119,10 +119,8 @@ class WsdlDownloaderTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$this->startPhpWebserver();
|
$this->startPhpWebserver();
|
||||||
|
|
||||||
$options = array(
|
$curl = new Curl();
|
||||||
'resolve_xsd_includes' => true,
|
$wd = new WsdlDownloader($curl);
|
||||||
);
|
|
||||||
$wd = new WsdlDownloader($options);
|
|
||||||
|
|
||||||
$class = new \ReflectionClass($wd);
|
$class = new \ReflectionClass($wd);
|
||||||
$method = $class->getMethod('resolveXsdIncludes');
|
$method = $class->getMethod('resolveXsdIncludes');
|
||||||
|
@ -176,7 +174,8 @@ class WsdlDownloaderTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
public function testResolveRelativePathInUrl()
|
public function testResolveRelativePathInUrl()
|
||||||
{
|
{
|
||||||
$wd = new WsdlDownloader();
|
$curl = new Curl();
|
||||||
|
$wd = new WsdlDownloader($curl);
|
||||||
|
|
||||||
$class = new \ReflectionClass($wd);
|
$class = new \ReflectionClass($wd);
|
||||||
$method = $class->getMethod('resolveRelativePathInUrl');
|
$method = $class->getMethod('resolveRelativePathInUrl');
|
||||||
|
|
Loading…
Reference in New Issue