Created
March 26, 2014 08:57
-
-
Save dominikzogg/9779137 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleSoapClient | |
{ | |
/** | |
* @var string | |
*/ | |
protected $username; | |
/** | |
* @var string | |
*/ | |
protected $password; | |
/** | |
* @param $username | |
* @param $password | |
*/ | |
public function __construct($username, $password) | |
{ | |
$this->username = (string) $username; | |
$this->password = (string) $password; | |
} | |
/** | |
* @param $soapUrl | |
* @param $soapAction | |
* @param $requestBody | |
* @return string | |
* @throws \ErrorException | |
*/ | |
public function request($soapUrl, $soapAction, $requestBody) | |
{ | |
$requestContext = self::prepareStreamContext($soapAction, $requestBody); | |
$level = error_reporting(0); | |
$responseContent = file_get_contents($soapUrl, null, $requestContext); | |
error_reporting($level); | |
if (false === $responseContent) { | |
$error = error_get_last(); | |
throw new \ErrorException($error['message']); | |
} | |
return $responseContent; | |
} | |
/** | |
* @return string | |
*/ | |
protected function getBasicAuth() | |
{ | |
return base64_encode($this->username . ':' . $this->password); | |
} | |
/** | |
* @param $soapAction | |
* @param $requestBody | |
* @return resource | |
*/ | |
protected function prepareStreamContext($soapAction, $requestBody) | |
{ | |
return stream_context_create(array( | |
'http' => array( | |
'method' => 'POST', | |
'header' => implode("\r\n", array( | |
'Connection: Close', | |
'User-Agent: PHP-SOAP/' . phpversion(), | |
'Content-Type: text/xml; charset=utf-8', | |
'SOAPAction: "' . $soapAction . '"', | |
'Content-Length: '. strlen($requestBody), | |
'Authorization: Basic ' . $this->getBasicAuth(), | |
)), | |
'content' => $requestBody, | |
'protocol_version' => '1.0', | |
'ignore_errors' => false, | |
'max_redirects' => 10, | |
'timeout'=> 5, | |
), | |
'ssl' => array( | |
'verify_peer' => false, | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment