Code

Updated RPC client
[gosa.git] / gosa-core / include / class_jsonRPC.inc
1 <?php
2 class jsonRPC {
4     private $curlHandler = NULL;
5     private $debug;
6     private $url;
7     private $id;
9     public function __construct($url,$debug = false) {
11         $this->url = $url;
12         $this->dbug = $debug;
13         $this->id = 1;
15         // Init Curl handler
16         $this->curlHandler = curl_init($this->url);
17         curl_setopt($this->curlHandler, CURLOPT_URL , $this->url);
18         curl_setopt($this->curlHandler, CURLOPT_COOKIESESSION , TRUE);
19         curl_setopt($this->curlHandler, CURLOPT_COOKIEFILE, 'cookiefile.txt'); 
20         curl_setopt($this->curlHandler, CURLOPT_POST , TRUE);
21         curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER , TRUE);
22         curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER , array('Content-Type: application/json'));
23     }
25     public function __call($method,$params) {
27         if (!is_scalar($method))  trigger_error('jsonRPC::__call requires a scalar value as first parameter!');
29         if (is_array($params)) {
30             $params = array_values($params);
31         } else {
32             trigger_error('jsonRPC::__call requires an array value as second parameter!');
33         }
35         // prepares the request
36         $this->id ++;
37         $request = json_encode(array('method' => $method,'params' => $params,'id' => $this->id));
39         // Set curl options
40         curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS , $request);
41         $response = curl_exec($this->curlHandler);        
42         $response = json_decode($response,true);
44         // Check responce id
45         if ($response['id'] != $this->id){
46             trigger_error('jsonPRC returned incorrect response id (request id: '.$this->id.', response id: '.$response['id'].')');
47         }
49         return($response['result']);
50     }
51 }
52 ?>