Skip to content

Instantly share code, notes, and snippets.

@jferrao
Last active November 23, 2021 07:00
Show Gist options
  • Save jferrao/bd7941e5dd50e271a52a to your computer and use it in GitHub Desktop.
Save jferrao/bd7941e5dd50e271a52a to your computer and use it in GitHub Desktop.
PHP SoapClient extension to persist any non http or non port 80 calls.
<?php
/**
* Allows the PHP SoapClient to persist any non http or non port 80 calls.
*
* When SoapClient in WSDL mode with a scheme or a port others then http and 80, the first
* connection goes through correctly but all subsequent ones revert back to http and no
* port in the URI. This class fixes that and allows the SoapClient to use the same scheme
* and port as the ones defined when a new instance of the class is created.
*
* @author João Ferrão
* @link https://github.com/jferrao
*/
final class MySoapClient extends SoapClient
{
/**
* The initial scheme value
* @var string
*/
protected $scheme = null;
/**
* The inital port value
* @var int
*/
protected $port = null;
/**
* Intercept the constructor to set the initial $scheme and $port values.
*
* @param string $wsdl URI of the WSDL file or null if working in non-WSDL mode
* @param array $options An array of options
*/
public function __construct($wsdl, array $options = array())
{
$parts = parse_url($wsdl);
// Store initial scheme
if (isset($parts['scheme'])) {
$this->scheme = $parts['scheme'];
}
// Store initial port
if (isset($parts['port'])) {
$this->port = $parts['port'];
}
return parent::__construct($wsdl, $options);
}
/**
* Overload and intercept all calls to this methods so it can rewrite $location scheme and port.
*
* @param string $request The XML SOAP request
* @param string $location The URL to request
* @param string $action The SOAP action
* @param int $version The SOAP version
* @param int $one_way If one_way is set to 1, this method returns nothing
* @return string The XML SOAP response
*/
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
return parent::__doRequest($request, $this->rewriteUrl($location), $action, $version, $one_way);
}
/**
* Rebuild the URL parts and rewrite the URL with initial scheme and port.
*
* @param string $location The URL string
* @return string The new URL with initial scheme and port
*/
private function rewriteUrl($location)
{
$parts = parse_url($location);
$url = '';
// Rebuild the URL based on all of it's components
(null === $this->scheme) || $url .= $this->scheme . '://';
(!isset($parts['user'])) || $url .= $parts['user'];
(!isset($parts['pass'])) || $url .= ':' . $parts['pass'];
(!isset($parts['user']) && !isset($parts['pass'])) || $url .= '@';
(!isset($parts['host'])) || $url .= $parts['host'];
(null === $this->port) || $url .= ':' . $this->port;
(!isset($parts['path'])) || $url .= $parts['path'];
(!isset($parts['query'])) || $url .= '?' . $parts['query'];
(!isset($parts['fragment'])) || $url .= '#' . $parts['fragment'];
return $url;
}
}
@rabhab20
Copy link

Thanks, helped a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment