1 <?php
2 /*
3 COPYRIGHT
5 Copyright 2007 Sergio Vaccaro <sergio@inservibile.org>
7 This file is part of JSON-RPC PHP.
9 JSON-RPC PHP is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 JSON-RPC PHP is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with JSON-RPC PHP; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
24 /**
25 * The object of this class are generic jsonRPC 1.0 clients
26 * http://json-rpc.org/wiki/specification
27 *
28 * @author sergio <jsonrpcphp@inservibile.org>
29 */
30 class jsonRPCClient {
32 private $curlHandler = NULL;
34 /**
35 * Debug state
36 *
37 * @var boolean
38 */
39 private $debug;
41 /**
42 * The server URL
43 *
44 * @var string
45 */
46 private $url;
47 /**
48 * The request id
49 *
50 * @var integer
51 */
52 private $id;
53 /**
54 * If true, notifications are performed instead of requests
55 *
56 * @var boolean
57 */
58 private $notification = false;
60 /**
61 * Takes the connection parameters
62 *
63 * @param string $url
64 * @param boolean $debug
65 */
66 public function __construct($url,$debug = false) {
67 // server URL
68 $this->url = $url;
69 // proxy
70 empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
71 // debug state
72 empty($debug) ? $this->debug = false : $this->debug = true;
73 // message id
74 $this->id = 1;
76 // Init Curl handler
77 $this->curlHandler = curl_init($this->url);
78 curl_setopt($this->curlHandler, CURLOPT_URL , $this->url);
79 curl_setopt($this->curlHandler, CURLOPT_COOKIESESSION , TRUE);
80 curl_setopt($this->curlHandler, CURLOPT_COOKIEFILE, 'cookiefile.txt');
81 }
83 /**
84 * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
85 *
86 * @param boolean $notification
87 */
88 public function setRPCNotification($notification) {
89 empty($notification) ?
90 $this->notification = false
91 :
92 $this->notification = true;
93 }
95 /**
96 * Performs a jsonRCP request and gets the results as an array
97 *
98 * @param string $method
99 * @param array $params
100 * @return array
101 */
102 public function __call($method,$params) {
104 // check
105 if (!is_scalar($method)) {
106 throw new Exception('Method name has no scalar value');
107 }
109 // check
110 if (is_array($params)) {
111 // no keys
112 $params = array_values($params);
113 } else {
114 throw new Exception('Params must be given as array');
115 }
117 // sets notification or request task
118 if ($this->notification) {
119 $currentId = NULL;
120 } else {
121 $currentId = $this->id;
122 }
124 // prepares the request
125 $request = array(
126 'method' => $method,
127 'params' => $params,
128 'id' => $currentId
129 );
130 $request = json_encode($request);
131 $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
133 curl_setopt($this->curlHandler, CURLOPT_POST , TRUE);
134 curl_setopt($this->curlHandler, CURLOPT_POSTFIELDS , $request);
135 curl_setopt($this->curlHandler, CURLOPT_RETURNTRANSFER , TRUE);
136 curl_setopt($this->curlHandler, CURLOPT_HTTPHEADER , array('Content-Type: application/json'));
137 $response = curl_exec($this->curlHandler);
138 $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
139 $response = json_decode($response,true);
141 // debug output
142 if ($this->debug) {
143 echo nl2br($debug);
144 }
146 // final checks and return
147 if (!$this->notification) {
148 // check
149 if ($response['id'] != $currentId) {
150 throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
151 }
152 if (!is_null($response['error'])) {
153 throw new Exception('Request error: '.$response['error']);
154 }
156 return $response['result'];
158 } else {
159 return true;
160 }
161 }
162 }
163 ?>