Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to fetch the WSDL with NTLM Auth. #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class SoapClient extends \SoapClient
*/
protected $options;

/**
* Cache for fetched WSDLs.
* @var array
*/
protected static $wsdlCache = [];

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -55,6 +61,8 @@ public function __construct($wsdl, array $options = [])
);
$this->options = $options;

$wsdl = $this->___fetchWSDL($wsdl);

// Verify that a user name and password were entered.
if (empty($options['user']) || empty($options['password'])) {
throw new \BadMethodCallException(
Expand All @@ -65,6 +73,35 @@ public function __construct($wsdl, array $options = [])
parent::__construct($wsdl, $options);
}

/**
* Fetch the WSDL to use.
*
* We need to fetch the WSDL on our own and save it into a file so that the parent class can load it from there.
* This is because the parent class doesn't support overwriting the WSDL fetching code which means we can't add
* the required NTLM handling.
*/
protected function ___fetchWSDL($wsdl) {
if (!empty($wsdl) && !file_exists($wsdl)) {
$wsdlHash = md5($wsdl);
if (empty(self::$wsdlCache[$wsdlHash])) {
$temp_file = sys_get_temp_dir() . '/' . $wsdlHash . '.ntlm.wsdl';
if (!file_exists($temp_file) || (isset($this->options['cache_wsdl']) && $this->options['cache_wsdl'] === WSDL_CACHE_NONE)) {
$wsdl_contents = $this->__doRequest(NULL , $wsdl, NULL, SOAP_1_1);
// Ensure the WSDL is only stored after validating it roughly.
if (!curl_errno($this->ch) && strpos($wsdl_contents, '<definitions ') !== FALSE) {
file_put_contents($temp_file, $wsdl_contents);
}
else {
throw new \SoapFault('Fetching WSDL', sprintf('Unable to fetch a valid WSDL definition from: %s', $wsdl));
}
}
self::$wsdlCache[$wsdlHash] = $temp_file;
}
$wsdl = self::$wsdlCache[$wsdlHash];
}
return $wsdl;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -135,6 +172,14 @@ public function getResponseCode()
*/
protected function buildHeaders($action)
{
if (is_null($action)) {
return array(
'Method: GET',
'Connection: Keep-Alive',
'User-Agent: PHP-SOAP-CURL',
'Content-Type: text/xml; charset=utf-8',
);
}
return array(
'Method: POST',
'Connection: Keep-Alive',
Expand Down