ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilRpcClient.php
Go to the documentation of this file.
1 <?php
2 include_once './Services/WebServices/RPC/classes/class.ilRpcClientException.php';
3 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
31 {
33  protected $url;
35  protected $prefix = '';
37  protected $timeout = 0;
39  protected $encoding = '';
40 
49  public function __construct($a_url, $a_prefix = '', $a_timeout = 0, $a_encoding = 'utf-8')
50  {
51  if (!extension_loaded('xmlrpc')) {
52  ilLoggerFactory::getLogger('wsrv')->error('RpcClient Xmlrpc extension not enabled');
53  throw new ilRpcClientException('Xmlrpc extension not enabled.', 50);
54  }
55 
56  $this->url = (string) $a_url;
57  $this->prefix = (string) $a_prefix;
58  $this->timeout = (int) $a_timeout;
59  $this->encoding = (string) $a_encoding;
60  }
61 
70  public function __call($a_method, $a_params)
71  {
72  //prepare xml post data
73  $method_name = str_replace('_', '.', $this->prefix . $a_method);
74  $rpc_options = array(
75  'verbosity' => 'newlines_only',
76  'escaping' => 'markup'
77  );
78 
79  if ($this->encoding) {
80  $rpc_options['encoding'] = $this->encoding;
81  }
82 
83  $post_data = xmlrpc_encode_request($method_name, $a_params, $rpc_options);
84 
85  //try to connect to the given url
86  try {
87  include_once './Services/WebServices/Curl/classes/class.ilCurlConnection.php';
88  $curl = new ilCurlConnection($this->url);
89  $curl->init();
90  $curl->setOpt(CURLOPT_HEADER, 'Content-Type: text/xml');
91  $curl->setOpt(CURLOPT_POST, (strlen($post_data) > 0));
92  $curl->setOpt(CURLOPT_POSTFIELDS, $post_data);
93  $curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
94 
95  if ($this->timeout > 0) {
96  $curl->setOpt(CURLOPT_TIMEOUT, $this->timeout);
97  }
98  ilLoggerFactory::getLogger('wsrv')->info('RpcClient request to ' . $this->url . ' / ' . $method_name);
99  $xml_resp = $curl->exec();
100  } catch (ilCurlConnectionException $e) {
101  ilLoggerFactory::getLogger('wsrv')->error('RpcClient could not connect to ' . $this->url . ' Reason ' . $e->getCode() . ': ' . $e->getMessage());
102  throw new ilRpcClientException($e->getMessage(), $e->getCode());
103  }
104 
105  //prepare output, throw exception if rpc fault is detected
106  $resp = xmlrpc_decode($xml_resp, $this->encoding);
107 
108  //xmlrpc_is_fault can just handle arrays as response
109  if (is_array($resp) && xmlrpc_is_fault($resp)) {
110  ilLoggerFactory::getLogger('wsrv')->error('RpcClient recieved error ' . $resp['faultCode'] . ': ' . $resp['faultString']);
111  include_once './Services/WebServices/RPC/classes/class.ilRpcClientException.php';
112  throw new ilRpcClientException('RPC-Server returned fault message: ' . $resp['faultString'], $resp['faultCode']);
113  }
114 
115  return $resp;
116  }
117 }
Class ilRpcClient.
Class ilRpcClientException.
__call($a_method, $a_params)
Magic caller to all RPC functions.
__construct($a_url, $a_prefix='', $a_timeout=0, $a_encoding='utf-8')
ilRpcClient constructor.
static getLogger($a_component_id)
Get component logger.