19declare(strict_types=1);
61 $this->
logger = $DIC->logger()->wsrv();
73 public function __call(
string $method, array $parameters): string|bool|stdClass
76 $method_name = str_replace(
'_',
'.', $this->prefix . $method);
84 $curl->setOpt(CURLOPT_HTTPHEADER, [
'Content-Type: text/xml']);
85 $curl->setOpt(CURLOPT_POST, (strlen($post_data) > 0));
86 $curl->setOpt(CURLOPT_POSTFIELDS, $post_data);
87 $curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
89 if ($this->timeout > 0) {
90 $curl->setOpt(CURLOPT_TIMEOUT, $this->timeout);
92 $this->
logger->debug(
'RpcClient request to ' . $this->url .
' / ' . $method_name);
93 $xml_response = $curl->exec();
96 'RpcClient could not connect to ' . $this->url .
' ' .
97 'Reason ' .
$e->getCode() .
': ' .
$e->getMessage()
112 $xml =
new DOMDocument(
'1.0',
'UTF-8');
113 $method_call = $xml->createElement(
'methodCall');
114 $method_name = $xml->createElement(
'methodName', $method);
115 $params = $xml->createElement(
'params');
117 foreach ($parameters as $parameter) {
119 is_string($parameter) => $encoded_parameter = $this->
encodeString($parameter),
120 is_int($parameter) => $encoded_parameter = $this->
encodeInteger($parameter),
121 is_bool($parameter) => $encoded_parameter = $this->
encodeBoolean($parameter),
124 'Invalid parameter type, only string, int, bool, and int[] are supported.'
127 $params->appendChild($xml->importNode($this->wrapParameter($encoded_parameter)->documentElement,
true));
130 $method_call->appendChild($method_name);
131 $method_call->appendChild(
$params);
133 $xml->appendChild($method_call);
134 return $xml->saveXML();
139 if (!is_array($parameter)) {
142 foreach ($parameter as $entries) {
143 if (!is_int($entries)) {
150 protected function wrapParameter(DOMDocument $encoded_parameter): DOMDocument
152 $xml =
new DOMDocument(
'1.0',
'UTF-8');
153 $param = $xml->createElement(
'param');
154 $value = $xml->createElement(
'value');
156 $value->appendChild($xml->importNode($encoded_parameter->documentElement,
true));
157 $param->appendChild($value);
159 $xml->appendChild(
$param);
165 $xml =
new DOMDocument(
'1.0',
'UTF-8');
166 $xml->appendChild($xml->createElement(
'string', $parameter));
172 $xml =
new DOMDocument(
'1.0',
'UTF-8');
173 $xml->appendChild($xml->createElement(
'int', (
string) $parameter));
179 $xml =
new DOMDocument(
'1.0',
'UTF-8');
180 $xml->appendChild($xml->createElement(
'boolean', $parameter ?
'1' :
'0'));
186 $xml =
new DOMDocument(
'1.0',
'UTF-8');
187 $array = $xml->createElement(
'array');
188 $data = $xml->createElement(
'data');
190 foreach ($parameters as $parameter) {
191 $value = $xml->createElement(
'value');
192 $value->appendChild($xml->importNode($this->encodeInteger($parameter)->documentElement,
true));
193 $data->appendChild($value);
195 $array->appendChild(
$data);
197 $xml->appendChild($array);
207 $response =
new DOMDocument(
'1.0',
'UTF-8');
215 $response_body =
$response->documentElement->childNodes->item(0);
217 if ($response_body ===
null) {
221 $this->
logger->dump($response_body);
223 return match ($response_body->nodeName) {
226 default =>
throw new ilRpcClientException(
'Unexpected element in response: ' . get_class($response_body)),
232 $param_child = $response_body->getElementsByTagName(
'value')->item(0)?->childNodes?->item(0);
234 if ($param_child ===
null) {
238 return match ($param_child->nodeName) {
243 default =>
throw new ilRpcClientException(
'Unexpected element in response value: ' . $param_child->nodeName),
249 return (
string) $string->nodeValue;
254 return (
object) base64_decode((
string) $base64->nodeValue);
259 return (
bool) $boolean->nodeValue;
268 $fault_string =
null;
270 $members = $response_body->getElementsByTagName(
'member');
271 foreach ($members as $member) {
272 $name = $member->getElementsByTagName(
'name')->item(0)?->nodeValue;
273 if ($name ===
'faultCode') {
274 if ($fault_code !==
null) {
277 $fault_code = (
int) $member->getElementsByTagName(
'int')->item(0)?->nodeValue;
279 if ($name ===
'faultString') {
280 if ($fault_string !==
null) {
283 $fault_string = $member->getElementsByTagName(
'string')->item(0)?->nodeValue;
287 if ($fault_code ===
null || $fault_string ===
null) {
291 $this->
logger->error(
'RpcClient recieved error ' . $fault_code .
': ' . $fault_string);
293 'RPC-Server returned fault message: ' .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Component logger with individual log levels by component id.
Class ilRpcClientException.
isListOfIntegers(mixed $parameter)
encodeRequest(string $method, array $parameters)
__construct(string $url, string $prefix='', int $timeout=0)
wrapParameter(DOMDocument $encoded_parameter)
encodeListOfIntegers(int ... $parameters)
decodeBoolean(DOMNode $boolean)
encodeInteger(int $parameter)
__call(string $method, array $parameters)
handleResponse(string $xml)
Returns decoded response if not faulty, otherwise throws exception.
decodeBase64(DOMNode $base64)
encodeBoolean(bool $parameter)
decodeOKResponse(DOMElement $response_body)
decodeString(DOMNode $string)
handleFaultResponse(DOMElement $response_body)
encodeString(string $parameter)
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params