Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request ondrejnov#27 from mrceperka/master
Browse files Browse the repository at this point in the history
SoapClient - save last response
  • Loading branch information
ondrejnov authored Jan 23, 2017
2 parents 880d3e5 + f0ced4a commit 2bd3337
Showing 1 changed file with 138 additions and 107 deletions.
245 changes: 138 additions & 107 deletions src/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@
use RobRichards\XMLSecLibs\XMLSecurityKey;

class SoapClient extends \SoapClient {

/** @var string */
private $key;

/** @var string */
private $cert;

/** @var boolean */
private $traceRequired;

/** @var float */
private $connectionStartTime;

/** @var float */
private $lastResponseStartTime;

/** @var float */
private $lastResponseEndTime;

/** @var string */
private $lastRequest;

/** @var string */
private $key;
/** @var string */
private $cert;
/** @var boolean */
private $traceRequired;
/** @var float */
private $connectionStartTime;
/** @var float */
private $lastResponseStartTime;
/** @var float */
private $lastResponseEndTime;
/** @var string */
private $lastRequest;
private $returnRequest = FALSE;

/**
* @var int timeout in milliseconds
*/
Expand All @@ -40,67 +40,77 @@ class SoapClient extends \SoapClient {
* @var int connection timeout in milliseconds
*/
private $connectTimeout = 2000;

/**
*
* @param string $service
* @param string $key
* @param string $cert
* @param boolean $trace
*/
public function __construct($service, $key, $cert, $trace = FALSE) {
$this->connectionStartTime = microtime(TRUE);
parent::__construct($service, [
'exceptions' => TRUE,
'trace' => $trace
]);
$this->key = $key;
$this->cert = $cert;
$this->traceRequired = $trace;
}


/**
* @var string
*/
private $lastResponse;

/**
* @var string
*/
private $lastResponseBody;

/**
*
* @param string $service
* @param string $key
* @param string $cert
* @param boolean $trace
*/
public function __construct($service, $key, $cert, $trace = FALSE) {
$this->connectionStartTime = microtime(TRUE);
parent::__construct($service, [
'exceptions' => TRUE,
'trace' => $trace
]);
$this->key = $key;
$this->cert = $cert;
$this->traceRequired = $trace;
}

public function getXML($request) {

$doc = new \DOMDocument('1.0');
$doc->loadXML($request);

$objWSSE = new WSSESoap($doc);
$objWSSE->addTimestamp();

$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, ['type' => 'private']);
$objKey->loadKey($this->key, TRUE);
$objWSSE->signSoapDoc($objKey, ["algorithm" => XMLSecurityDSig::SHA256]);

$token = $objWSSE->addBinaryToken(file_get_contents($this->cert));
$objWSSE->attachTokentoSig($token);

return $objWSSE->saveXML();
}

public function getXMLforMethod($method, $data) {
$this->returnRequest = TRUE;
$this->$method($data);
$this->returnRequest = FALSE;
return $this->lastRequest;
}

public function __doRequest($request, $location, $saction, $version, $one_way = NULL) {

$xml = $this->getXML($request);
public function __doRequest($request, $location, $saction, $version, $one_way = NULL) {
$xml = $this->getXML($request);
$this->lastRequest = $xml;
if ($this->returnRequest) {
return '';
}

$this->traceRequired && $this->lastResponseStartTime = microtime(TRUE);

$this->traceRequired && $this->lastResponseStartTime = microtime(TRUE);
$response = $this->__doRequestByCurl($xml, $location, $saction, $version);

$this->traceRequired && $this->lastResponseEndTime = microtime(TRUE);

return $response;
}

$this->traceRequired && $this->lastResponseEndTime = microtime(TRUE);
return $response;
}
/**
* @param string $request
* @param string $location
Expand Down Expand Up @@ -137,21 +147,23 @@ public function __doRequestByCurl($request, $location, $action, $version, $one_w
$options = $this->__curlSetTimeoutOption($options, $this->timeout, 'CURLOPT_TIMEOUT');
// ConnectTimeout in milliseconds
$options = $this->__curlSetTimeoutOption($options, $this->connectTimeout, 'CURLOPT_CONNECTTIMEOUT');

$this->__setCurlOptions($curl, $options);
$response = curl_exec($curl);

$this->lastResponse = $response;

if (curl_errno($curl)) {
$errorMessage = curl_error($curl);
$errorNumber = curl_errno($curl);
curl_close($curl);
throw new ClientException($errorMessage, $errorNumber);
}

$header_len = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_len);
$body = substr($response, $header_len);

$this->lastResponseBody = $body;

curl_close($curl);
// Return?
if ($one_way) {
Expand All @@ -160,7 +172,7 @@ public function __doRequestByCurl($request, $location, $action, $version, $one_w
return $body;
}
}

private function __setCurlOptions($curl, array $options)
{
foreach ($options as $option => $value) {
Expand All @@ -172,7 +184,7 @@ private function __setCurlOptions($curl, array $options)
);
}
}

private function __curlSetTimeoutOption($options, $milliseconds, $name)
{
if ($milliseconds > 0) {
Expand All @@ -188,45 +200,45 @@ private function __curlSetTimeoutOption($options, $milliseconds, $name)
}
return $options;
}


/**
*
* @return float
*/
public function __getLastResponseTime() {
return $this->lastResponseEndTime - $this->lastResponseStartTime;
}

/**
*
* @return float
*/
public function __getConnectionTime($tillLastRequest = FALSE) {
return $tillLastRequest ? $this->getConnectionTimeTillLastRequest() : $this->getConnectionTimeTillNow();
}

private function getConnectionTimeTillLastRequest() {
if (!$this->lastResponseEndTime || !$this->connectionStartTime) {
return NULL;
}
return $this->lastResponseEndTime - $this->connectionStartTime;
}

private function getConnectionTimeTillNow() {
if (!$this->connectionStartTime) {
return NULL;
}
return microtime(TRUE) - $this->connectionStartTime;
}

/**
* @return string
*/
public function __getLastRequest() {
return $this->lastRequest;
}

*
* @return float
*/
public function __getLastResponseTime() {
return $this->lastResponseEndTime - $this->lastResponseStartTime;
}
/**
*
* @return float
*/
public function __getConnectionTime($tillLastRequest = FALSE) {
return $tillLastRequest ? $this->getConnectionTimeTillLastRequest() : $this->getConnectionTimeTillNow();
}
private function getConnectionTimeTillLastRequest() {
if (!$this->lastResponseEndTime || !$this->connectionStartTime) {
return NULL;
}
return $this->lastResponseEndTime - $this->connectionStartTime;
}
private function getConnectionTimeTillNow() {
if (!$this->connectionStartTime) {
return NULL;
}
return microtime(TRUE) - $this->connectionStartTime;
}
/**
* @return string
*/
public function __getLastRequest() {
return $this->lastRequest;
}
/**
* @param int|null $milliseconds timeout in milliseconds
*/
Expand Down Expand Up @@ -255,6 +267,25 @@ public function getConnectTimeout()
{
return $this->connectTimeout;
}



/**
* @return mixed
*/
public function __getLastResponse()
{
return $this->lastResponse;
}

/**
* @return mixed
*/
public function __getLastResponseBody()
{
return $this->lastResponseBody;
}





}

0 comments on commit 2bd3337

Please sign in to comment.