-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSoapClient.php
297 lines (250 loc) · 6.98 KB
/
SoapClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Ondrejnov\EET;
use Ondrejnov\EET\Exceptions\ClientException;
use RobRichards\WsePhp\WSSESoap;
use RobRichards\XMLSecLibs\XMLSecurityDSig;
use RobRichards\XMLSecLibs\XMLSecurityKey;
class SoapClient extends \SoapClient {
/** @var string */
private $key;
/** @var string */
private $passphrase;
/** @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
*/
private $timeout = 2500;
/**
* @var int connection timeout in milliseconds
*/
private $connectTimeout = 2000;
/**
* @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, $passphrase = NULL) {
$this->connectionStartTime = microtime(TRUE);
parent::__construct($service, [
'exceptions' => TRUE,
'trace' => $trace
]);
$this->key = $key;
$this->cert = $cert;
$this->traceRequired = $trace;
$this->passphrase = $passphrase;
}
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']);
if ($this->passphrase) {
$objKey->passphrase = $this->passphrase;
}
$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);
$this->lastRequest = $xml;
if ($this->returnRequest) {
return '';
}
$this->traceRequired && $this->lastResponseStartTime = microtime(TRUE);
$response = $this->__doRequestByCurl($xml, $location, $saction, $version);
$this->traceRequired && $this->lastResponseEndTime = microtime(TRUE);
return $response;
}
/**
* @param string $request
* @param string $location
* @param string $action
* @param int $version
* @param bool $one_way
*
* @return string|null
* @throws ClientException
*/
public function __doRequestByCurl($request, $location, $action, $version, $one_way = FALSE)
{
// Call via Curl and use the timeout a
$curl = curl_init($location);
if ($curl === false) {
throw new ClientException('Curl initialisation failed');
}
/** @var $headers array of headers to be sent with request */
$headers = array(
'User-Agent: PHP-SOAP',
'Content-Type: text/xml; charset=utf-8',
'SOAPAction: "' . $action . '"',
'Content-Length: ' . strlen($request),
);
$options = array(
CURLOPT_VERBOSE => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request,
CURLOPT_HEADER => $headers,
CURLOPT_HTTPHEADER => array(sprintf('Content-Type: %s', $version == 2 ? 'application/soap+xml' : 'text/xml'), sprintf('SOAPAction: %s', $action)),
);
// Timeout in milliseconds
$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) {
return null;
}
return $body;
}
private function __setCurlOptions($curl, array $options)
{
foreach ($options as $option => $value) {
if (false !== curl_setopt($curl, $option, $value)) {
continue;
}
throw new ClientException(
sprintf('Failed setting CURL option %d (%s) to %s', $option, $this->__getCurlOptionName($option), var_export($value, true))
);
}
}
private function __curlSetTimeoutOption($options, $milliseconds, $name)
{
if ($milliseconds > 0) {
if (defined("{$name}_MS")) {
$options[constant("{$name}_MS")] = $milliseconds;
} else {
$seconds = ceil($milliseconds / 1000);
$options[$name] = $seconds;
}
if ($milliseconds <= 1000) {
$options[CURLOPT_NOSIGNAL] = 1;
}
}
return $options;
}
/**
*
* @return float
*/
public function __getLastResponseTime() {
if (!$this->lastResponseEndTime || !$this->lastResponseStartTime) {
return NULL;
}
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
*/
public function setTimeout($milliseconds)
{
$this->timeout = $milliseconds;
}
/**
* @return int|null timeout in milliseconds
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* @param int|null $milliseconds
*/
public function setConnectTimeout($milliseconds)
{
$this->connectTimeout = $milliseconds;
}
/**
* @return int|null
*/
public function getConnectTimeout()
{
return $this->connectTimeout;
}
/**
* @return mixed
*/
public function __getLastResponse()
{
return $this->lastResponse;
}
/**
* @return mixed
*/
public function __getLastResponseBody()
{
return $this->lastResponseBody;
}
}