ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilRpcClient.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
47{
48 protected string $url;
49 protected string $prefix = '';
50 protected int $timeout = 0;
51 protected string $encoding = '';
52
53 protected ilLogger $logger;
54
63 public function __construct(string $a_url, string $a_prefix = '', int $a_timeout = 0, string $a_encoding = 'utf-8')
64 {
65 global $DIC;
66
67 $this->logger = $DIC->logger()->wsrv();
68
69 if (!extension_loaded('xmlrpc')) {
70 ilLoggerFactory::getLogger('wsrv')->error('RpcClient Xmlrpc extension not enabled');
71 throw new ilRpcClientException('Xmlrpc extension not enabled.', 50);
72 }
73
74 $this->url = $a_url;
75 $this->prefix = $a_prefix;
76 $this->timeout = $a_timeout;
77 $this->encoding = $a_encoding;
78 }
79
88 public function __call(string $a_method, array $a_params)
89 {
90 //prepare xml post data
91 $method_name = str_replace('_', '.', $this->prefix . $a_method);
92 $rpc_options = array(
93 'verbosity' => 'newlines_only',
94 'escaping' => 'markup'
95 );
96
97 if ($this->encoding) {
98 $rpc_options['encoding'] = $this->encoding;
99 }
100 $post_data = xmlrpc_encode_request($method_name, $a_params, $rpc_options);
101 //try to connect to the given url
102 try {
103 $curl = new ilCurlConnection($this->url);
104 $curl->init(false);
105 $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: text/xml']);
106 $curl->setOpt(CURLOPT_POST, (strlen($post_data) > 0));
107 $curl->setOpt(CURLOPT_POSTFIELDS, $post_data);
108 $curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
109
110 if ($this->timeout > 0) {
111 $curl->setOpt(CURLOPT_TIMEOUT, $this->timeout);
112 }
113 $this->logger->debug('RpcClient request to ' . $this->url . ' / ' . $method_name);
114 $xml_resp = $curl->exec();
115 } catch (ilCurlConnectionException $e) {
116 $this->logger->error(
117 'RpcClient could not connect to ' . $this->url . ' ' .
118 'Reason ' . $e->getCode() . ': ' . $e->getMessage()
119 );
120 throw new ilRpcClientException($e->getMessage(), $e->getCode());
121 }
122
123 //prepare output, throw exception if rpc fault is detected
124 $resp = xmlrpc_decode($xml_resp, $this->encoding);
125
126 //xmlrpc_is_fault can just handle arrays as response
127 if (is_array($resp) && xmlrpc_is_fault($resp)) {
128 $this->logger->error('RpcClient recieved error ' . $resp['faultCode'] . ': ' . $resp['faultString']);
129 throw new ilRpcClientException(
130 'RPC-Server returned fault message: ' .
131 $resp['faultString'],
132 $resp['faultCode']
133 );
134 }
135 return $resp;
136 }
137}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
Class ilRpcClientException.
Class ilRpcClient.
__construct(string $a_url, string $a_prefix='', int $a_timeout=0, string $a_encoding='utf-8')
ilRpcClient constructor.
__call(string $a_method, array $a_params)
Magic caller to all RPC functions.
global $DIC
Definition: shib_login.php:26