1
1
<?php
2
2
/*
3
3
COPYRIGHT
4
-
4
+
5
5
Copyright 2007 Sergio Vaccaro <[email protected] >
6
-
6
+
7
7
This file is part of JSON-RPC PHP.
8
-
8
+
9
9
JSON-RPC PHP is free software; you can redistribute it and/or modify
10
10
it under the terms of the GNU General Public License as published by
11
11
the Free Software Foundation; either version 2 of the License, or
12
12
(at your option) any later version.
13
-
13
+
14
14
JSON-RPC PHP is distributed in the hope that it will be useful,
15
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
17
GNU General Public License for more details.
18
-
18
+
19
19
You should have received a copy of the GNU General Public License
20
20
along with JSON-RPC PHP; if not, write to the Free Software
21
21
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
27
*
28
28
* @author sergio <[email protected] >
29
29
*/
30
- class jsonRPCClient {
30
+ class jsonRPCClient
31
+ {
31
32
32
33
/**
33
34
* Debug state
@@ -61,8 +62,9 @@ class jsonRPCClient {
61
62
* @param string $url
62
63
* @param boolean $debug
63
64
*/
64
- public function __construct ($ url ,$ debug = false ) {
65
- // server URL
65
+ public function __construct ($ url , $ debug = false )
66
+ {
67
+ // server URL
66
68
$ this ->url = $ url ;
67
69
// proxy
68
70
empty ($ proxy ) ? $ this ->proxy = '' : $ this ->proxy = $ proxy ;
@@ -77,8 +79,9 @@ public function __construct($url,$debug = false) {
77
79
*
78
80
* @param boolean $notification
79
81
*/
80
- public function setRPCNotification ($ notification ) {
81
- empty ($ notification ) ?
82
+ public function setRPCNotification ($ notification )
83
+ {
84
+ empty ($ notification ) ?
82
85
$ this ->notification = false
83
86
:
84
87
$ this ->notification = true ;
@@ -91,79 +94,75 @@ public function setRPCNotification($notification) {
91
94
* @param array $params
92
95
* @return array
93
96
*/
94
- public function __call ($ method ,$ params ) {
97
+ public function __call ($ method , $ params )
98
+ {
95
99
96
100
// check
97
101
if (!is_scalar ($ method )) {
98
- throw new Exception ('Method name has no scalar value ' );
102
+ throw new Exception ('Method name has no scalar value ' );
99
103
}
100
104
101
105
// check
102
106
if (is_array ($ params )) {
103
- // no keys
107
+ // no keys
104
108
$ params = array_values ($ params );
105
109
} else {
106
- throw new Exception ('Params must be given as array ' );
110
+ throw new Exception ('Params must be given as array ' );
107
111
}
108
112
109
113
// sets notification or request task
110
114
if ($ this ->notification ) {
111
- $ currentId = NULL ;
115
+ $ currentId = null ;
112
116
} else {
113
- $ currentId = $ this ->id ;
117
+ $ currentId = $ this ->id ;
114
118
}
115
119
116
- $ request = array ('jsonrpc ' => '2.0 ' ,
120
+ $ request = array ('jsonrpc ' => '2.0 ' ,
117
121
'method ' => $ method ,
118
122
'params ' => $ params
119
123
);
120
124
121
- if ($ method == "send_transaction " OR $ method == "get_transaction " OR $ method == "transfer " ){
125
+ if ($ method == "send_transaction " or $ method == "get_transaction " or $ method == "transfer " ) {
122
126
$ request = str_replace (array ('[ ' , '] ' ), '' , htmlspecialchars (json_encode ($ request ), ENT_NOQUOTES ));
123
127
$ this ->debug && $ this ->debug .='***** Request ***** ' ."\n" .$ request ."\n" .'***** End Of request ***** ' ."\n\n" ;
124
128
125
- if (preg_match ("/ \"destinations \":{/i " ,$ request )){
126
- $ request = str_replace ("\"destinations \":{ " , "\"destinations \":[{ " , $ request );
127
- $ request = str_replace ("}, \"payment " ,"}], \"payment " , $ request );
128
- }
129
-
130
- }else {
131
- $ request = json_encode ($ request ,JSON_FORCE_OBJECT );
129
+ if (preg_match ("/ \"destinations \":{/i " , $ request )) {
130
+ $ request = str_replace ("\"destinations \":{ " , "\"destinations \":[{ " , $ request );
131
+ $ request = str_replace ("}, \"payment " , "}], \"payment " , $ request );
132
132
}
133
+ } else {
134
+ $ request = json_encode ($ request , JSON_FORCE_OBJECT );
135
+ }
133
136
134
137
// performs the HTTP POST
135
138
$ ch = curl_init ($ this ->url );
136
- curl_setopt ($ ch ,CURLOPT_RETURNTRANSFER , true );
137
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Content-type: application/json ' ));
138
- curl_setopt ($ ch , CURLOPT_POST , true );
139
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ request );
140
- $ response = json_decode (curl_exec ($ ch ),true );
141
- curl_close ($ ch );
139
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
140
+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ('Content-type: application/json ' ));
141
+ curl_setopt ($ ch , CURLOPT_POST , true );
142
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ request );
143
+ $ response = json_decode (curl_exec ($ ch ), true );
144
+ curl_close ($ ch );
142
145
//print_r($response);
143
146
144
147
// debug output
145
148
if ($ this ->debug ) {
146
- echo nl2br ($ debug );
149
+ echo nl2br ($ debug );
147
150
}
148
151
149
152
// final checks and return
150
153
if (!$ this ->notification ) {
151
-
152
-
153
- if (!is_array ($ response )) {
154
- echo "Can't connect to wallet. Please try again later. " ;
155
- exit ();
156
- }
154
+ if (!is_array ($ response )) {
155
+ echo "Can't connect to wallet. Please try again later. " ;
156
+ exit ();
157
+ }
157
158
158
- if (array_key_exists ("error " ,$ response )) {
159
- return $ response ['error ' ]['message ' ];
160
- }
161
-
162
- return $ response ['result ' ];
159
+ if (array_key_exists ("error " , $ response )) {
160
+ return $ response ['error ' ]['message ' ];
161
+ }
163
162
163
+ return $ response ['result ' ];
164
164
} else {
165
- return true ;
165
+ return true ;
166
166
}
167
167
}
168
168
}
169
- ?>
0 commit comments