ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilSoapClient.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
6 
15 {
16  public const DEFAULT_CONNECT_TIMEOUT = 10;
17  public const DEFAULT_RESPONSE_TIMEOUT = 5;
18 
19  private ilLogger $log;
21  private ?SoapClient $client = null;
22  private string $uri;
23  private bool $use_wsdl = true;
24  private int $connect_timeout = self::DEFAULT_CONNECT_TIMEOUT;
25  private int $response_timeout = self::DEFAULT_RESPONSE_TIMEOUT;
26  private ?int $stored_socket_timeout = null;
27 
28  public function __construct(string $a_uri = '')
29  {
30  global $DIC;
31 
32  $this->settings = $DIC->settings();
33  $this->log = $DIC->logger()->wsrv();
34  $this->uri = $a_uri;
35  $this->use_wsdl = true;
36  $timeout = (int) $this->settings->get('soap_connect_timeout', (string) self::DEFAULT_CONNECT_TIMEOUT);
37  if ($timeout) {
38  $this->connect_timeout = $timeout;
39  }
40  $this->connect_timeout = $timeout;
41 
42  $this->response_timeout = (int) $this->settings->get('soap_response_timeout', (string) self::DEFAULT_RESPONSE_TIMEOUT);
43  }
44 
45  public function getServer(): string
46  {
47  return $this->uri;
48  }
49 
50  public function setTimeout(int $a_timeout): void
51  {
52  $this->connect_timeout = $a_timeout;
53  }
54 
55  public function getTimeout(): int
56  {
58  }
59 
60  public function setResponseTimeout(int $a_timeout): void
61  {
62  $this->response_timeout = $a_timeout;
63  }
64 
65  public function getResponseTimeout(): int
66  {
68  }
69 
70  public function enableWSDL(bool $a_stat): void
71  {
72  $this->use_wsdl = $a_stat;
73  }
74 
75  public function enabledWSDL(): bool
76  {
77  return $this->use_wsdl;
78  }
79 
80  public function init(): bool
81  {
82  $internal_path = $this->settings->get('soap_internal_wsdl_path');
83  if (trim($this->getServer()) === '') {
84  if ($internal_path) {
85  $this->uri = $internal_path;
86  } elseif (trim($this->settings->get('soap_wsdl_path', '')) !== '') {
87  $this->uri = $this->settings->get('soap_wsdl_path', '');
88  } else {
89  $this->uri = ilUtil::_getHttpPath() . '/public/soap/server.php?wsdl';
90  }
91  }
92  try {
93  $this->log->debug('Using wsdl: ' . $this->getServer());
94  $this->log->debug('Using connection timeout: ' . $this->getTimeout());
95  $this->log->debug('Using response timeout: ' . $this->getResponseTimeout());
96 
97  $this->setSocketTimeout(true);
98  $this->client = new SoapClient(
99  $this->uri,
100  array(
101  'exceptions' => true,
102  'trace' => 1,
103  'connection_timeout' => $this->getTimeout(),
104  'stream_context' => $this->uri === $internal_path ? stream_context_create([
105  'ssl' => [
106  'verify_peer' => (bool) $this->settings->get('soap_internal_wsdl_verify_peer', '1'),
107  'verify_peer_name' => (bool) $this->settings->get('soap_internal_wsdl_verify_peer_name', '1'),
108  'allow_self_signed' => (bool) $this->settings->get('soap_internal_wsdl_allow_self_signed', ''),
109  ]
110  ]) : null
111  )
112  );
113  return true;
114  } catch (SoapFault $ex) {
115  $this->log->warning('Soap init failed with message: ' . $ex->getMessage());
116  $this->resetSocketTimeout();
117  return false;
118  } finally {
119  $this->resetSocketTimeout();
120  }
121  }
122 
123  protected function setSocketTimeout(bool $a_wsdl_mode): bool
124  {
125  $this->stored_socket_timeout = (int) ini_get('default_socket_timeout');
126  $this->log->debug('Default socket timeout is: ' . $this->stored_socket_timeout);
127 
128  if ($a_wsdl_mode) {
129  $this->log->debug('WSDL mode, using socket timeout: ' . $this->getTimeout());
130  ini_set('default_socket_timeout', (string) $this->getTimeout());
131  } else {
132  $this->log->debug('Non WSDL mode, using socket timeout: ' . $this->getResponseTimeout());
133  ini_set('default_socket_timeout', (string) $this->getResponseTimeout());
134  }
135 
136  return true;
137  }
138 
142  protected function resetSocketTimeout(): bool
143  {
144  ini_set('default_socket_timeout', (string) $this->stored_socket_timeout);
145  $this->log->debug('Restoring default socket timeout to: ' . $this->stored_socket_timeout);
146  return true;
147  }
148 
154  public function call(string $a_operation, array $a_params)
155  {
156  $this->log->debug('Calling webservice: ' . $a_operation);
157 
158  $this->setSocketTimeout(false);
159  try {
160  return $this->client->__call($a_operation, $a_params);
161  } catch (SoapFault $exception) {
162  $this->log->error('Calling webservice failed with message: ' . $exception->getMessage());
163  $this->log->debug((string) $this->client->__getLastResponseHeaders());
164  $this->log->debug((string) $this->client->__getLastResponse());
165  return false;
166  } catch (Exception $exception) {
167  $this->log->error('Caught unknown exception with message: ' . $exception->getMessage());
168  $this->log->debug((string) $this->client->__getLastResponseHeaders());
169  $this->log->debug((string) $this->client->__getLastResponse());
170  } finally {
171  $this->resetSocketTimeout();
172  }
173 
174  return false;
175  }
176 }
resetSocketTimeout()
Reset socket default timeout to defaults.
const DEFAULT_RESPONSE_TIMEOUT
enableWSDL(bool $a_stat)
SoapClient $client
const DEFAULT_CONNECT_TIMEOUT
setResponseTimeout(int $a_timeout)
setTimeout(int $a_timeout)
call(string $a_operation, array $a_params)
global $DIC
Definition: shib_login.php:25
__construct(string $a_uri='')
setSocketTimeout(bool $a_wsdl_mode)
client()
description: > This example shows how a Progress Bar can be rendered and used on the client...
Definition: client.php:21
static _getHttpPath()