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
25 * The object of this class are generic jsonRPC 1.0 clients
26 * http://json-rpc.org/wiki/specification
28 * @author sergio <jsonrpcphp@inservibile.org>
52 * If true, notifications are performed instead of requests
56 private $notification = false;
59 * Takes the connection parameters
62 * @param boolean $debug
64 public function __construct($url,$debug = false) {
68 empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
70 empty($debug) ? $this->debug = false : $this->debug = true;
76 * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
78 * @param boolean $notification
80 public function setRPCNotification($notification) {
81 empty($notification) ?
82 $this->notification = false
84 $this->notification = true;
88 * Performs a jsonRCP request and gets the results as an array
90 * @param string $method
91 * @param array $params
94 public function __call($method,$params) {
97 if (!is_scalar($method)) {
98 throw new Exception('Method name has no scalar value');
102 if (is_array($params)) {
104 $params = array_values($params);
106 throw new Exception('Params must be given as array');
109 // sets notification or request task
110 if ($this->notification) {
113 $currentId = $this->id;
116 // prepares the request
122 $request = json_encode($request);
123 $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
125 // performs the HTTP POST using curl
127 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
128 curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
129 curl_setopt($curl, CURLOPT_URL, $this->url);
130 curl_setopt($curl, CURLOPT_POST, TRUE);
131 curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
132 $response = curl_exec($curl);
137 throw new Exception('Unable to connect to '.$this->url, 0);
140 $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
141 $response = json_decode($response,true);
148 // final checks and return
149 if (!$this->notification) {
151 if ($response['id'] != $currentId) {
152 throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
154 if (!is_null($response['error'])) {
155 $error = $response['error'];
156 throw new Exception($error['message'], $error["code"]);
159 return $response['result'];