-
Notifications
You must be signed in to change notification settings - Fork 1
/
oraclesoapapi.php
555 lines (512 loc) · 15.8 KB
/
oraclesoapapi.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
/**
* @package: oracleSoapApi
* @link: https://github.com/feltkamptv/oraclechatapi/
* @developer: Feltkamp.tv Multimedia Productions
* @email: [email protected]
* @tel: +31 (0) 20 785 4487
* @website: http://www.feltkamp.tv
* @author: Pim Feltkamp
* @copyright 2016 Pim Feltkamp
* @license: http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @note: This program is distributed in the hope that it will be useful - WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
**/
class oracleSoapApi{
protected $wsdl = false;
protected $username = false;
protected $password = false;
protected $appId = false;
protected $sessionId = false;
protected $interfaceName = false;
protected $interfaceId = false;
public function __construct($wsdl, $username, $password, $appId, $sessionId, $interfaceId, $interfaceName){
$this->wsdl = $wsdl;
$this->username = $username;
$this->password = $password;
$this->appId = $appId;
$this->sessionId = $sessionId;
$this->interfaceId = $interfaceId;
$this->interfaceName = $interfaceName;
$this->error = false;
$this->error_curl = false;
$this->httpcode = false;
$this->retry = false;
$this->clientTransactionID = 0;
$this->chatSessionToken = false;
$this->sitename = false;
}// end function
/**
* SET WSDL
*
* This function will set the wsdl parameter.
*
* @param string $wsdl
*
* @return: true
**/
public function setWSDL($wsdl){
$this->wsdl = $wsdl;
return true;
}// end function
/**
* SET SESSION ID
*
* This function will set the sessionId parameter.
*
* @param string $id
*
* @return: true
**/
public function setSessionID($id){
$this->sessionId = $id;
return true;
}// end function
/**
* SET CHAT SESSION TOKEN
*
* This function will set the chatSessionToken parameter.
*
* @param string $token
*
* @return: true
**/
public function setChatSessionToken($token){
$this->chatSessionToken = $token;
return true;
}// end function
/**
* SET SITENAME
*
* This function will set the sitename parameter.
*
* @param string $sitename
*
* @return: true
**/
public function setSitename($sitename){
$this->sitename = $sitename;
return true;
}// end function
/**
* CHECK AVAILIBILITY
*
* This function will check the availability of the Chat Service.
* The Chat service will be available if it is in operating hours and not in holiday
*
* @return: boolean
**/
public function checkAvailability(){
$opening_hours = $this->call('GetChatOperatingHours', array());
$opening_hours = $this->parseResult('GetChatOperatingHours', $opening_hours);
if($opening_hours['in_holiday'][0] == 'false' && $opening_hours['in_operating_hours'][0] == 'true'){
return true;
}else{
return false;
}
}// end function
/**
* GET CHAT URL
*
* This function will perform the GetChatUrl Soap API call.
* It will return the chat url, chat token and sitename if the call was successfull.
*
* @return: array on success, false on failure.
**/
public function getChatUrl(){
$chat_soap = $this->call('GetChatUrl', array('UrlType' => 'ENDUSER'));
if(!$chat_soap){
return false;
}
$chat_soap = $this->parseResult('GetChatUrl', $chat_soap);
if(!$chat_soap){
return false;
}else{
return $chat_soap;
}
}// end function
/**
* REQUEST CHAT
*
* This function will call the chat url, set the wsdl, sitename and token and perform the RequestChat SOAP API call.
*
* @param string $phonenumber, array $data
*
* @return: string $session_id on success, false on failure
**/
public function requestChat($phonenumber, $data){
$chat_soap = $this->getChatUrl();
if(!$chat_soap){
return false;
}
$this->setWSDL($chat_soap['chat_url']);
$this->setSitename($chat_soap['sitename']);
$this->setChatSessionToken($chat_soap['chat_token']);
$request_chat = $this->call('RequestChat', $data);
if($this->error){
return false;
}else{
$request_chat = $this->parseResult('RequestChat', $request_chat);
return $request_chat['session_id'];
}
}// end function
/**
* REQUEST CHAT ONLY
*
* This function will only perform the RequestChat SOAP API call.
*
* @param string $phonenumber, array $data
*
* @return: string $session_id on success, false on failure
**/
public function requestChatOnly($phonenumber, $data){
$request_chat = $this->call('RequestChat', $data);
if($this->error){
return false;
}else{
$request_chat = $this->parseResult('RequestChat', $request_chat);
return $request_chat['session_id'];
}
}// end function
/**
* RESUME CHAT
*
* This function will call the chat url, set the wsdl, sitename and token.
*
* @param string $phonenumber, array $data
*
* @return: boolean
**/
public function resumeChat($phonenumber, $data){
$chat_soap = $this->getChatUrl();
if(!$chat_soap){
return false;
}
$this->setWSDL($chat_soap['chat_url']);
$this->setSitename($chat_soap['sitename']);
$this->setChatSessionToken($chat_soap['chat_token']);
return true;
}// end function
/**
* SEND MSG
*
* This function will perform the PostChatMessage SOAP API call.
*
* @param string $msg
*
* @return: boolean
**/
public function sendMsg($msg){
$post_message = $this->call('PostChatMessage', array('Body'=>$msg));
if(!$post_message){
return false;
}else{
return true;
}
}// end function
/**
* CHECK ACTIVE CHAT
*
* This function will check if a chat is still active and available.
*
* @return: boolean
**/
public function checkActiveChat(){
$post = array('Mode'=>'LISTENING');
$post_message = $this->call('SendActivityChange', $post);
if(!$post_message){
return false;
}else{
return true;
}
}// end function
/**
* SET TYPING MESSAGE
*
* This function will perform the SendActivityChange SOAP API call.
*
* @param boolean $typing
*
* @return: boolean
**/
public function setTypingMessage($typing){
if($typing){
$post = array('Mode'=>'RESPONDING');
}else{
$post = array('Mode'=>'LISTENING');
}
$post_message = $this->call('SendActivityChange', $post);
if(!$post_message){
return false;
}else{
return true;
}
}// end function
/**
* GET REQUEST HEADER
*
* This function will create the request header for a SOAP API call.
*
* @param string $action
*
* @return: string $header
**/
public function getRequestHeader($action){
$header = '<soapenv:Header>';
if($action == 'GetChatUrl' || $action == 'GetChatOperatingHours'){
$header .= '<v1:ClientRequestHeader><v1:AppID>'.$this->appId.'</v1:AppID></v1:ClientRequestHeader>';
}else{
$header .= '<v1:ChatClientInfoHeader><v1:AppID>'.$this->appId.'</v1:AppID>';
if($this->sessionId){
$header .= '<v1:SessionID>'.$this->sessionId.'</v1:SessionID>';
}
$header .= '</v1:ChatClientInfoHeader>';
}
$header .= '<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" mustUnderstand="1"><wsse:UsernameToken><wsse:Username>'.$this->username.'</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$this->password.'</wsse:Password></wsse:UsernameToken></wsse:Security></soapenv:Header>';
return $header;
}// end function
/**
* GET TRANSACTION REQUEST DATA
*
* This function will create the TransactionRequestData for a SOAP API call.
*
* @return: string $result
**/
public function getTransactionRequestData(){
$result = '<v11:TransactionRequestData>
<v11:ClientRequestTime>'.date("c").'</v11:ClientRequestTime>
<v11:ClientTransactionID>'.$this->getClientTransactionID().'</v11:ClientTransactionID>
<v11:SiteName>'.$this->sitename.'</v11:SiteName>
</v11:TransactionRequestData>';
return $result;
}// end function
/**
* GET CLIENT TRANSACTION ID
*
* This function will count up the clientTransactionID and return the new number.
*
* @return: int $this->clientTransactionID
**/
public function getClientTransactionID(){
$this->clientTransactionID++;
return $this->clientTransactionID;
}// end function
/**
* GET SOAP REQUEST
*
* This function will create the SOAP request data for a SOAP API call.
*
* @param string $action, array $params
*
* @return: string $raw_xml
**/
public function getSoapRequest($action, $params){
if($action == 'GetChatUrl' || $action == 'GetChatOperatingHours'){
$raw_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="urn:messages.chat.ws.rightnow.com/v1_1">'.$this->getRequestHeader($action).'<soapenv:Body>';
}else{
$raw_xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="urn:messages.common.chat.ws.rightnow.com/v1" xmlns:v11="urn:messages.enduser.chat.ws.rightnow.com/v1">'.$this->getRequestHeader($action).'<soapenv:Body>';
}
switch($action){
case 'RequestChat':
$raw_xml .= '<v11:RequestChat>'.$this->getTransactionRequestData();
$raw_xml .= '<v11:CustomerInformation>';
if(array_key_exists('EMailAddress', $params['CustomerInformation'])){
$raw_xml .= '<v1:EMailAddress>'.$params['CustomerInformation']['EMailAddress'].'</v1:EMailAddress>';
}
if(array_key_exists('FirstName', $params['CustomerInformation'])){
$raw_xml .= '<v1:FirstName>'.$params['CustomerInformation']['FirstName'].'</v1:FirstName>';
}
if(array_key_exists('LastName', $params['CustomerInformation'])){
$raw_xml .= '<v1:LastName>'.$params['CustomerInformation']['LastName'].'</v1:LastName>';
}
if(array_key_exists('ContactID', $params['CustomerInformation'])){
$raw_xml .= '<v1:ContactID id="'.$params['CustomerInformation']['ContactID'].'"/>';
}
$raw_xml .= '<v1:InterfaceID><v1:ID id="'.$this->interfaceId.'"/><v1:Name>'.$this->interfaceName.'</v1:Name></v1:InterfaceID>';
$raw_xml .= '</v11:CustomerInformation>';
$raw_xml .= '<v11:ChatSessionToken>'.$this->chatSessionToken.'</v11:ChatSessionToken>';
$raw_xml .= '</v11:RequestChat>';
break;
case 'GetChatUrl':
$raw_xml .= '<v1:GetChatUrl><v1:UrlType>'.$params['UrlType'].'</v1:UrlType></v1:GetChatUrl>';
break;
case 'PostChatMessage':
$raw_xml .= '<v11:PostChatMessage>'.$this->getTransactionRequestData();
$raw_xml .= '<v11:Body>'.$params['Body'].'</v11:Body></v11:PostChatMessage>';
break;
case 'TerminateChat':
$raw_xml .= '<v11:TerminateChat>'.$this->getTransactionRequestData();
$raw_xml .= '</v11:TerminateChat>';
break;
case 'RetrieveMessages':
$raw_xml .= '<v11:RetrieveMessages>'.$this->getTransactionRequestData();
$raw_xml .= '</v11:RetrieveMessages>';
break;
case 'SendActivityChange':
$raw_xml .= '<v11:SendActivityChange>'.$this->getTransactionRequestData();
$raw_xml .= '<v11:Mode>'.$params['Mode'].'</v11:Mode></v11:SendActivityChange>';
break;
case 'GetChatOperatingHours':
$raw_xml .= '<v1:GetChatOperatingHours/>';
break;
default:
if(count($params) > 0){
$raw_xml .= '<v1:'.$action.'>';
foreach($params as $param_key => $param_val){
if(is_array($param_val)){
$raw_xml .= '<v1:'.$param_key.'>';
foreach($param_val as $param_val_key => $param_val_val){
$raw_xml .= '<v1:'.$param_val_key.'>'.$param_val_val.'</v1:'.$param_val_key.'>';
}// foreach
$raw_xml .= '</v1:'.$param_key.'>';
}else{
$raw_xml .= '<v1:'.$param_key.'>'.$param_val.'</v1:'.$param_key.'>';
}
}// foreach
$raw_xml .= '</v1:'.$action.'>';
}else{
$raw_xml .= '<v1:'.$action.'/>';
}
break;
}//switch
$raw_xml .= '</soapenv:Body></soapenv:Envelope>';
return $raw_xml;
}// end function
/**
* PARSE RESULT
*
* This function will parse the result from a SOAP API call.
*
* @param string $action, string $response
*
* @return: array $result
**/
public function parseResult($action, $response){
$result = array();
if(!$response){
return $result;
}
$xml = simplexml_load_string($response, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
switch($action){
case 'RequestChat':
$soap = $xml->children($ns['S']);
$res = $soap->Body->children($ns['ns3']);
$result['session_id'] = (string) $res->RequestChatResponse->SessionID;
break;
case 'GetChatUrl':
$soap = $xml->children($ns['soapenv']);
$res = $soap->Body->children($ns['n0']);
$result['chat_url'] = (string) $res->GetChatUrlResponse->GetChatUrlResult->ChatUrl;
$result['chat_token'] = (string) $res->GetChatUrlResponse->GetChatUrlResult->ChatToken;
$result['sitename'] = (string) $res->GetChatUrlResponse->GetChatUrlResult->SiteName;
break;
case 'GetChatOperatingHours':
$soap = $xml->children($ns['soapenv']);
$res = $soap->Body->children($ns['n0']);
$result['opening_hours'] = (array) $res->GetChatOperatingHoursResponse->GetChatOperatingHoursResult->Intervals->Interval;
$result['in_operating_hours'] = (array) $res->GetChatOperatingHoursResponse->GetChatOperatingHoursResult->InOperatingHours;
$result['in_holiday'] = (array) $res->GetChatOperatingHoursResponse->GetChatOperatingHoursResult->InHoliday;
break;
case 'PostChatMessage':
break;
case 'TerminateChat':
break;
case 'RetrieveMessages':
$soap = $xml->children($ns['S']);
$res = $soap->Body->children($ns['ns3']);
$result['messages'] = (array) $res->RetrieveMessagesResponse->SystemMessages->RNChatMessagePostedMessage;
break;
case 'SendActivityChange':
break;
default:
$soap = $xml->children($ns['S']);
$result = (array) $soap->Body->children($ns['ns3']);
break;
}//switch
return $result;
}// end function
/**
* CALL
*
* This function will perform the SOAP API call with Curl.
*
* @param string $action, array $params, string $request (optional)
*
* @return: string $response
**/
public function call($action, $params, $request = false){
$this->error = false;
$this->error_curl = false;
if(!$request){
$this->request = $this->getSoapRequest($action, $params);
}else{
$this->request = $request;
}
$headers = array(
'Content-Type: text/xml;charset=UTF-8',
'Connection: Keep-Alive',
'SOAPAction: "'.$action.'"',
'Accept-Encoding: gzip,deflate',
'User-Agent: Apache-HttpClient/4.1.1 (java 1.5)',
'Cache-Control: no-cache',
'Pragma: no-cache',
'Content-length: '.strlen($this->request),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $this->wsdl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if(!$response){
$this->error_curl = curl_error($ch);
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code == 200){
$this->retry = false;
return $this->decodeGzip($response);
}elseif(empty($http_code) && !$this->retry){
$this->retry = true;
sleep(500000);
return $this->call($action, $params, $request);
}else{
$this->retry = false;
$this->httpcode = $http_code;
$this->error = $this->decodeGzip($response);
return false;
}
}// end function
/**
* DECODE GZIP
*
* This function will decode a gzipped string, but only if it is gzipped.
*
* @param string $response
*
* @return: string $response
**/
public function decodeGzip($response){
$is_gzip = 0 === mb_strpos($response, "\x1f" . "\x8b" . "\x08", 0, "US-ASCII");
if($is_gzip){
return gzdecode($response);
}else{
return $response;
}
}// end function
/**
* GET ERROR
*
* This function will return the error output.
*
* @return: string
**/
public function getError(){
return $this->error;
}// end function
}// end class