ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  function __construct ($a_url, $a_prefix = '', $a_timeout = 0, $a_encoding = 'utf-8')
50  {
51  if(!extension_loaded('xmlrpc'))
52  {
53  ilLoggerFactory::getLogger('wsrv')->error('RpcClient Xmlrpc extension not enabled');
54  throw new ilRpcClientException('Xmlrpc extension not enabled.', 50);
55  }
56 
57  $this->url = (string) $a_url;
58  $this->prefix = (string) $a_prefix;
59  $this->timeout = (int) $a_timeout;
60  $this->encoding = (string) $a_encoding;
61  }
62 
71  public function __call($a_method, $a_params)
72  {
73  //prepare xml post data
74  $method_name = str_replace('_', '.', $this->prefix . $a_method);
75  $rpc_options = array(
76  'verbosity'=>'newlines_only',
77  'escaping' => 'markup'
78  );
79 
80  if($this->encoding)
81  {
82  $rpc_options['encoding'] = $this->encoding;
83  }
84 
85  $post_data = xmlrpc_encode_request($method_name, $a_params,$rpc_options);
86 
87  //try to connect to the given url
88  try
89  {
90  include_once './Services/WebServices/Curl/classes/class.ilCurlConnection.php';
91  $curl = new ilCurlConnection($this->url);
92  $curl->init();
93  $curl->setOpt(CURLOPT_HEADER, 'Content-Type: text/xml');
94  $curl->setOpt(CURLOPT_POST, sizeof($post_data));
95  $curl->setOpt(CURLOPT_POSTFIELDS, $post_data);
96  $curl->setOpt(CURLOPT_RETURNTRANSFER , 1);
97 
98  if($this->timeout > 0)
99  {
100  $curl->setOpt(CURLOPT_TIMEOUT, $this->timeout);
101  }
102  ilLoggerFactory::getLogger('wsrv')->info('RpcClient request to ' . $this->url . ' / ' . $method_name);
103  $xml_resp = $curl->exec();
104  }
105  catch(ilCurlConnectionException $e)
106  {
107  ilLoggerFactory::getLogger('wsrv')->error('RpcClient could not connect to ' . $this->url . ' Reason ' . $e->getCode() . ': ' .$e->getMessage());
108  throw new ilRpcClientException($e->getMessage(), $e->getCode());
109  }
110 
111  //prepare output, throw exception if rpc fault is detected
112  $resp = xmlrpc_decode($xml_resp,$this->encoding);
113 
114  //xmlrpc_is_fault can just handle arrays as response
115  if(is_array($resp)&& xmlrpc_is_fault($resp))
116  {
117  ilLoggerFactory::getLogger('wsrv')->error('RpcClient recieved error '. $resp['faultCode'] . ': ' . $resp['faultString'] );
118  include_once './Services/WebServices/RPC/classes/class.ilRpcClientException.php';
119  throw new ilRpcClientException('RPC-Server returned fault message: '.$resp['faultString'], $resp['faultCode']);
120  }
121 
122  return $resp;
123  }
124 }
Add rich text string
The name of the decorator.
Class ilRpcClient.
Create styles array
The data for the language used.
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.