note 101503 deleted from soapclient.soapclient by cmb
| From: | cmb@php.net | Date: | Sun, 03 Feb 2019 18:03:39 +0000 |
| Subject: | note 101503 deleted from soapclient.soapclient by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210179@lists.php.net to get a copy of this message | ||
Note Submitter: faebu at faebu dot ch
----
I'm experiencing the same problems when trying to load a WDSL fiel which is protected by basic
http authentication, since the parameters login and password are just used for the request but not
when reading the wdsl file. I just use the following workaround by downloading the xml file to a
non-protected location on my server. Please notice that this doesn't support any kind of
caching.
Hope it helps
<?php
class SoapAuthClient extends SoapClient {
/**
* Since the PHP SOAP package does not support basic authentication
* this class downloads the WDSL file using the cURL package and
* creates a local copy of the wdsl on your server.
* Make sure you provide the following additional parameter in the
* $options Array:
* wdsl_local_copy => true
*/
private $cache_dir = '/home/example/htdocs/cache/';
private $cache_url = 'http://www.example.com/cache/';
function SoapAuthClient($wdsl, $options) {
if (isset($options['wdsl_local_copy']) &&
$options['wdsl_local_copy'] == true &&
isset($options['login']) &&
isset($options['password'])) {
$file = md5(uniqid()).'.xml';
if (($fp = fopen($this->cache_dir.$file, "w")) == false) {
throw new Exception('Could not create local WDSL file
('.$this->cache_dir.$file.')');
}
$ch = curl_init();
$credit = ($options['login'].':'.$options['password']);
curl_setopt($ch, CURLOPT_URL, $wdsl);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $credit);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FILE, $fp);
if (($xml = curl_exec($ch)) === false) {
//curl_close($ch);
fclose($fp);
unlink($this->cache_dir.$file);
throw new Exception(curl_error($ch));
}
curl_close($ch);
fclose($fp);
$wdsl = $this->cache_url.$file;
}
unset($options['wdsl_local_copy']);
unset($options['wdsl_force_local_copy']);
echo $wdsl;
parent::__construct($wdsl, $options);
unlink($this->cache_dir.$file);
}
}
?>