ILIAS  release_8 Revision v8.24
class.ilSoapClient.php
Go to the documentation of this file.
1<?php
2
3declare(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;
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 if (trim($this->getServer()) === '') {
83 if (trim($this->settings->get('soap_wsdl_path', '')) !== '') {
84 $this->uri = $this->settings->get('soap_wsdl_path', '');
85 } else {
86 $this->uri = ilUtil::_getHttpPath() . '/webservice/soap/server.php?wsdl';
87 }
88 }
89 try {
90 $this->log->debug('Using wsdl: ' . $this->getServer());
91 $this->log->debug('Using connection timeout: ' . $this->getTimeout());
92 $this->log->debug('Using response timeout: ' . $this->getResponseTimeout());
93
94 $this->setSocketTimeout(true);
95 $this->client = new SoapClient(
96 $this->uri,
97 array(
98 'exceptions' => true,
99 'trace' => 1,
100 'connection_timeout' => $this->getTimeout()
101 )
102 );
103 return true;
104 } catch (SoapFault $ex) {
105 $this->log->warning('Soap init failed with message: ' . $ex->getMessage());
106 $this->resetSocketTimeout();
107 return false;
108 } finally {
109 $this->resetSocketTimeout();
110 }
111 }
112
113 protected function setSocketTimeout(bool $a_wsdl_mode): bool
114 {
115 $this->stored_socket_timeout = (int) ini_get('default_socket_timeout');
116 $this->log->debug('Default socket timeout is: ' . $this->stored_socket_timeout);
117
118 if ($a_wsdl_mode) {
119 $this->log->debug('WSDL mode, using socket timeout: ' . $this->getTimeout());
120 ini_set('default_socket_timeout', (string) $this->getTimeout());
121 } else {
122 $this->log->debug('Non WSDL mode, using socket timeout: ' . $this->getResponseTimeout());
123 ini_set('default_socket_timeout', (string) $this->getResponseTimeout());
124 }
125
126 return true;
127 }
128
132 protected function resetSocketTimeout(): bool
133 {
134 ini_set('default_socket_timeout', (string) $this->stored_socket_timeout);
135 $this->log->debug('Restoring default socket timeout to: ' . $this->stored_socket_timeout);
136 return true;
137 }
138
144 public function call(string $a_operation, array $a_params)
145 {
146 $this->log->debug('Calling webservice: ' . $a_operation);
147
148 $this->setSocketTimeout(false);
149 try {
150 return $this->client->__call($a_operation, $a_params);
151 } catch (SoapFault $exception) {
152 $this->log->error('Calling webservice failed with message: ' . $exception->getMessage());
153 $this->log->debug((string) $this->client->__getLastResponseHeaders());
154 $this->log->debug((string) $this->client->__getLastResponse());
155 return false;
156 } catch (Exception $exception) {
157 $this->log->error('Caught unknown exception with message: ' . $exception->getMessage());
158 $this->log->debug((string) $this->client->__getLastResponseHeaders());
159 $this->log->debug((string) $this->client->__getLastResponse());
160 } finally {
161 $this->resetSocketTimeout();
162 }
163
164 return false;
165 }
166}
Component logger with individual log levels by component id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
call(string $a_operation, array $a_params)
setSocketTimeout(bool $a_wsdl_mode)
setTimeout(int $a_timeout)
const DEFAULT_CONNECT_TIMEOUT
SoapClient $client
setResponseTimeout(int $a_timeout)
enableWSDL(bool $a_stat)
resetSocketTimeout()
Reset socket default timeout to defaults.
__construct(string $a_uri='')
const DEFAULT_RESPONSE_TIMEOUT
static _getHttpPath()
global $DIC
Definition: feed.php:28