Skip to content

Commit 06747ca

Browse files
committed
PSR2 comaptible source, improved debugging.
1 parent bf33b80 commit 06747ca

2 files changed

Lines changed: 244 additions & 235 deletions

File tree

src/org/jsonrpcphp/JsonRPCClient.php

Lines changed: 176 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,188 @@
11
<?php
22
/*
3-
Copyright 2007 Sergio Vaccaro <sergio@inservibile.org>
4-
Copyright 2012, 2015 Johannes Weberhofer <jweberhofer@weberhofer.at>
5-
6-
This file is part of JSON-RPC PHP.
7-
8-
JSON-RPC PHP is free software; you can redistribute it and/or modify
9-
it under the terms of the GNU General Public License as published by
10-
the Free Software Foundation; either version 2 of the License, or
11-
(at your option) any later version.
12-
13-
JSON-RPC PHP is distributed in the hope that it will be useful,
14-
but WITHOUT ANY WARRANTY; without even the implied warranty of
15-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16-
GNU General Public License for more details.
17-
18-
You should have received a copy of the GNU General Public License
19-
along with JSON-RPC PHP; if not, write to the Free Software
20-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21-
*/
3+
* Copyright 2007 Sergio Vaccaro <sergio@inservibile.org>
4+
* Copyright 2012, 2015 Johannes Weberhofer <jweberhofer@weberhofer.at>
5+
*
6+
* This file is part of JSON-RPC PHP.
7+
*
8+
* JSON-RPC PHP is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* JSON-RPC PHP is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with JSON-RPC PHP; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
2222

2323
/**
2424
* The object of this class are generic jsonRPC 1.0 clients
25-
*
25+
*
2626
* @see http://json-rpc.org/wiki/specification
2727
* @license GPLv2+
2828
* @author sergio <jsonrpcphp@inservibile.org>
2929
* @author Johannes Weberhofer <jweberhofer@weberhofer.at>
3030
*/
31+
namespace org\jsonrpcphp;
32+
33+
class JsonRPCClient
34+
{
35+
36+
/**
37+
* Debug state
38+
*
39+
* @var boolean
40+
*/
41+
private $debug;
42+
43+
/**
44+
* The server URL
45+
*
46+
* @var string
47+
*/
48+
private $url;
49+
50+
/**
51+
* The request id
52+
*
53+
* @var integer
54+
*/
55+
private $id;
56+
57+
/**
58+
* If true, notifications are performed instead of requests
59+
*
60+
* @var boolean
61+
*/
62+
private $notification = false;
63+
64+
/**
65+
* Takes the connection parameters
66+
*
67+
* @param string $url
68+
* @param boolean $debug
69+
*/
70+
public function __construct($url, $debug = false)
71+
{
72+
// server URL
73+
$this->url = $url;
74+
// proxy
75+
empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
76+
// debug state
77+
empty($debug) ? $this->debug = false : $this->debug = true;
78+
// message id
79+
$this->id = 1;
80+
}
81+
82+
/**
83+
* Sets the notification state of the object.
84+
* In this state, notifications are performed, instead of requests.
85+
*
86+
* @param boolean $notification
87+
*/
88+
public function setRPCNotification($notification)
89+
{
90+
empty($notification) ? $this->notification = false : $this->notification = true;
91+
}
92+
93+
/**
94+
* Performs a jsonRCP request and gets the results as an array
95+
*
96+
* @param string $method
97+
* @param array $params
98+
* @return array
99+
*/
100+
public function __call($method, $params)
101+
{
102+
103+
// check
104+
if (! is_scalar($method)) {
105+
throw new \Exception('Method name has no scalar value');
106+
}
107+
108+
// check
109+
if (is_array($params)) {
110+
// no keys
111+
$params = array_values($params);
112+
} else {
113+
throw new \Exception('Params must be given as array');
114+
}
115+
116+
// sets notification or request task
117+
if ($this->notification) {
118+
$currentId = null;
119+
} else {
120+
$currentId = $this->id;
121+
}
122+
123+
// prepares the request
124+
$request = array(
125+
'method' => $method,
126+
'params' => $params,
127+
'id' => $currentId
128+
);
129+
$request = json_encode($request);
130+
if ($this->debug) {
131+
echo '***** Request *****' . "\n" . $request . "\n";
132+
}
133+
134+
// performs the HTTP POST
135+
$opts = array(
136+
'http' => array(
137+
'method' => 'POST',
138+
'header' => 'Content-type: application/json',
139+
'content' => $request
140+
)
141+
);
142+
$context = stream_context_create($opts);
143+
144+
if (is_callable('curl_init')) {
145+
// use curl when available; solves problems with allow_url_fopen
146+
$ch = curl_init($this->url);
147+
curl_setopt($ch, CURLOPT_POST, 1);
148+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
149+
'Content-type: application/json'
150+
));
151+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
152+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
153+
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
154+
$response = curl_exec($ch);
155+
if ($response === false) {
156+
throw new \Exception('Unable to connect to ' . $this->url);
157+
}
158+
} else {
159+
if ($fp = fopen($this->url, 'r', false, $context)) {
160+
$response = '';
161+
while ($row = fgets($fp)) {
162+
$response .= trim($row) . "\n";
163+
}
164+
} else {
165+
throw new \Exception('Unable to connect to ' . $this->url);
166+
}
167+
}
168+
if ($this->debug) {
169+
echo '***** Response *****' . "\n" . $response . "\n" . '***** End of Response *****' . "\n\n";
170+
}
171+
$response = json_decode($response, true);
172+
173+
// final checks and return
174+
if (! $this->notification) {
175+
// check
176+
if ($response['id'] != $currentId) {
177+
throw new \Exception('Incorrect response id: ' . $response['id'] . ' (request id: ' . $currentId . ')');
178+
}
179+
if (! is_null($response['error'])) {
180+
throw new \Exception('Request error: ' . $response['error']);
181+
}
31182

32-
namespace org\jsonrpcphp {
33-
34-
class JsonRPCClient {
35-
36-
/**
37-
* Debug state
38-
*
39-
* @var boolean
40-
*/
41-
private $debug;
42-
43-
/**
44-
* The server URL
45-
*
46-
* @var string
47-
*/
48-
private $url;
49-
/**
50-
* The request id
51-
*
52-
* @var integer
53-
*/
54-
private $id;
55-
/**
56-
* If true, notifications are performed instead of requests
57-
*
58-
* @var boolean
59-
*/
60-
private $notification = false;
61-
62-
/**
63-
* Takes the connection parameters
64-
*
65-
* @param string $url
66-
* @param boolean $debug
67-
*/
68-
public function __construct($url,$debug = false) {
69-
// server URL
70-
$this->url = $url;
71-
// proxy
72-
empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
73-
// debug state
74-
empty($debug) ? $this->debug = false : $this->debug = true;
75-
// message id
76-
$this->id = 1;
77-
}
78-
79-
/**
80-
* Sets the notification state of the object. In this state, notifications are performed, instead of requests.
81-
*
82-
* @param boolean $notification
83-
*/
84-
public function setRPCNotification($notification) {
85-
empty($notification) ?
86-
$this->notification = false
87-
:
88-
$this->notification = true;
89-
}
90-
91-
/**
92-
* Performs a jsonRCP request and gets the results as an array
93-
*
94-
* @param string $method
95-
* @param array $params
96-
* @return array
97-
*/
98-
public function __call($method,$params) {
99-
100-
// check
101-
if (!is_scalar($method)) {
102-
throw new \Exception('Method name has no scalar value');
103-
}
104-
105-
// check
106-
if (is_array($params)) {
107-
// no keys
108-
$params = array_values($params);
109-
} else {
110-
throw new \Exception('Params must be given as array');
111-
}
112-
113-
// sets notification or request task
114-
if ($this->notification) {
115-
$currentId = NULL;
116-
} else {
117-
$currentId = $this->id;
118-
}
119-
120-
// prepares the request
121-
$request = array(
122-
'method' => $method,
123-
'params' => $params,
124-
'id' => $currentId
125-
);
126-
$request = json_encode($request);
127-
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n";
128-
129-
// performs the HTTP POST
130-
$opts = array ('http' => array (
131-
'method' => 'POST',
132-
'header' => 'Content-type: application/json',
133-
'content' => $request
134-
));
135-
$context = stream_context_create($opts);
136-
137-
if(is_callable('curl_init')) {
138-
// use curl when available; solves problems with allow_url_fopen
139-
$ch = curl_init($this->url);
140-
curl_setopt($ch, CURLOPT_POST, 1);
141-
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
142-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
143-
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
144-
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
145-
$response = curl_exec($ch);
146-
if ( $response === false ) {
147-
throw new \Exception('Unable to connect to '.$this->url);
148-
}
149-
} else {
150-
if ($fp = fopen($this->url, 'r', false, $context)) {
151-
$response = '';
152-
while($row = fgets($fp)) {
153-
$response.= trim($row)."\n";
154-
}
155-
} else {
156-
throw new \Exception('Unable to connect to '.$this->url);
157-
}
158-
}
159-
$this->debug && $this->debug.='***** Response *****'."\n".$response."\n".'***** End of Response *****'."\n\n";
160-
$response = json_decode($response,true);
161-
162-
// debug output
163-
if ($this->debug) {
164-
echo print_r($this->debug);
165-
}
166-
167-
// final checks and return
168-
if (!$this->notification) {
169-
// check
170-
if ($response['id'] != $currentId) {
171-
throw new \Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
172-
}
173-
if (!is_null($response['error'])) {
174-
throw new \Exception('Request error: '.$response['error']);
175-
}
176-
177-
return $response['result'];
178-
179-
} else {
180-
return true;
181-
}
182-
}
183-
}
183+
return $response['result'];
184+
} else {
185+
return true;
186+
}
187+
}
184188
}
185-
?>

0 commit comments

Comments
 (0)