ILIAS  release_8 Revision v8.24
class.ilRpcClient.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
6
33{
34 protected string $url;
35 protected string $prefix = '';
36 protected int $timeout = 0;
37 protected string $encoding = '';
38
39 protected ilLogger $logger;
40
49 public function __construct(string $a_url, string $a_prefix = '', int $a_timeout = 0, string $a_encoding = 'utf-8')
50 {
51 global $DIC;
52
53 $this->logger = $DIC->logger()->wsrv();
54
55 if (!extension_loaded('xmlrpc')) {
56 ilLoggerFactory::getLogger('wsrv')->error('RpcClient Xmlrpc extension not enabled');
57 throw new ilRpcClientException('Xmlrpc extension not enabled.', 50);
58 }
59
60 $this->url = $a_url;
61 $this->prefix = $a_prefix;
62 $this->timeout = $a_timeout;
63 $this->encoding = $a_encoding;
64 }
65
74 public function __call(string $a_method, array $a_params)
75 {
76 //prepare xml post data
77 $method_name = str_replace('_', '.', $this->prefix . $a_method);
78 $rpc_options = array(
79 'verbosity' => 'newlines_only',
80 'escaping' => 'markup'
81 );
82
83 if ($this->encoding) {
84 $rpc_options['encoding'] = $this->encoding;
85 }
86 $post_data = xmlrpc_encode_request($method_name, $a_params, $rpc_options);
87 //try to connect to the given url
88 try {
89 $curl = new ilCurlConnection($this->url);
90 $curl->init(false);
91 $curl->setOpt(CURLOPT_HTTPHEADER, ['Content-Type: text/xml']);
92 $curl->setOpt(CURLOPT_POST, (strlen($post_data) > 0));
93 $curl->setOpt(CURLOPT_POSTFIELDS, $post_data);
94 $curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
95
96 if ($this->timeout > 0) {
97 $curl->setOpt(CURLOPT_TIMEOUT, $this->timeout);
98 }
99 $this->logger->debug('RpcClient request to ' . $this->url . ' / ' . $method_name);
100 $xml_resp = $curl->exec();
101 } catch (ilCurlConnectionException $e) {
102 $this->logger->error(
103 'RpcClient could not connect to ' . $this->url . ' ' .
104 'Reason ' . $e->getCode() . ': ' . $e->getMessage()
105 );
106 throw new ilRpcClientException($e->getMessage(), $e->getCode());
107 }
108
109 //prepare output, throw exception if rpc fault is detected
110 $resp = xmlrpc_decode($xml_resp, $this->encoding);
111
112 //xmlrpc_is_fault can just handle arrays as response
113 if (is_array($resp) && xmlrpc_is_fault($resp)) {
114 $this->logger->error('RpcClient recieved error ' . $resp['faultCode'] . ': ' . $resp['faultString']);
115 throw new ilRpcClientException(
116 'RPC-Server returned fault message: ' .
117 $resp['faultString'],
118 $resp['faultCode']
119 );
120 }
121 return $resp;
122 }
123}
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: feed.php:28