-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONAPI.php
More file actions
244 lines (214 loc) · 7.22 KB
/
JSONAPI.php
File metadata and controls
244 lines (214 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* A PHP class for access Minecraft servers that have Bukkit with the {@link http://github.com/alecgorge/JSONAPI JSONAPI} plugin installed.
*
* This class handles everything from key creation to URL creation to actually returning the decoded JSON as an associative array.
*
* @author Alec Gorge <alecgorge@gmail.com>
* @version Alpha 5
* @link http://github.com/alecgorge/JSONAPI
* @package JSONAPI
* @since Alpha 5
*/
class JSONAPI {
private $host;
private $port;
private $username;
private $password;
const URL_FORMAT = 'http://%s:%d/api/2/call?json=%s';
private $timeout;
/**
* Creates a new JSONAPI instance.
*/
public function __construct ($host, $port, $uname, $pword, $salt = '', $timeout = 10) {
$this->host = $host;
$this->port = $port;
$this->username = $uname;
$this->password = $pword;
$this->salt = $salt;
$this->timeout = $timeout;
}
/**
* Generates the proper SHA256 based key from the given method suitable for use as the key GET parameter in a JSONAPI API call.
*
* @param string $method The name of the JSONAPI API method to generate the key for.
* @return string The SHA256 key suitable for use as the key GET parameter in a JSONAPI API call.
*/
public function createKey($method) {
if(is_array($method)) {
$method = json_encode($method);
}
return hash('sha256', $this->username . $method . $this->password);
}
/**
* Generates the proper URL for a standard API call the given method and arguments.
*
* @param string $method The name of the JSONAPI API method to generate the URL for.
* @param array $args An array of arguments that are to be passed in the URL.
* @return string A proper standard JSONAPI API call URL. Example: "http://localhost:20059/api/call?method=methodName&args=jsonEncodedArgsArray&key=validKey".
*/
public function makeURL($method, array $args) {
return sprintf(self::URL_FORMAT, $this->host, $this->port, rawurlencode(json_encode($this->constructCall($method, $args))));
}
/**
* Generates the proper URL for a multiple API call the given method and arguments.
*
* @param array $methods An array of strings, where each string is the name of the JSONAPI API method to generate the URL for.
* @param array $args An array of arrays, where each array contains the arguments that are to be passed in the URL.
* @return string A proper multiple JSONAPI API call URL. Example: "http://localhost:20059/api/call-multiple?method=[methodName,methodName2]&args=jsonEncodedArrayOfArgsArrays&key=validKey".
*/
public function makeURLMultiple(array $methods, array $args) {
return sprintf(self::URL_FORMAT, $this->host, $this->port, rawurlencode(json_encode($this->constructCalls($methods, $args))));
}
/**
* Calls the single given JSONAPI API method with the given args.
*
* @param string $method The name of the JSONAPI API method to call.
* @param array $args An array of arguments that are to be passed.
* @return array An associative array representing the JSON that was returned.
*/
public function call($method, array $args = array()) {
if(is_array($method)) {
return $this->callMultiple($method, $args);
}
$url = $this->makeURL($method, $args);
return json_decode($this->curl($url), true);
}
private function curl($url) {
if(extension_loaded('cURL')) {
$c = curl_init($url);
curl_setopt($c, CURLOPT_PORT, $this->port);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_TIMEOUT, $this->timeout);
$result = curl_exec($c);
curl_close($c);
return $result;
}else{
$opts = array('http' =>
array(
'timeout' => $this->timeout
)
);
return file_get_contents($url, false, stream_context_create($opts));
}
}
private function constructCall($method, array $args) {
$json = array();
$json['name'] = $method;
$json['arguments'] = $args;
$json['key'] = $this->createKey($method);
$json['username'] = $this->username;
return $json;
}
private function constructCalls(array $methods, array $args) {
$calls = array();
foreach ($methods as $key => $method) {
$calls[] = $this->constructCall($method, $args[$key]);
}
return $calls;
}
/**
* Calls the given JSONAPI API methods with the given args.
*
* @param array $methods An array strings, where each string is the name of a JSONAPI API method to call.
* @param array $args An array of arrays of arguments that are to be passed.
* @throws Exception When the length of the $methods array and the $args array are different, an exception is thrown.
* @return array An array of associative arrays representing the JSON that was returned.
*/
public function callMultiple(array $methods, array $args = array()) {
if(count($methods) !== count($args)) {
throw new Exception(sprintf('The length of the arrays %s and %s are different! You need an array of arguments for each method!', $methods, $args));
}
$url = $this->makeURLMultiple($methods, $args);
return json_decode($this->curl($url), true);
}
/**
* The default function called if no one matched for JSONAPI.
*
* @param string $method The name of the JSONAPI API method to call.
* @param array $params An array of arguments that are to be passed.
* @return array An associative array representing the JSON that was returned.
*/
function __call($method, $params) {
if(is_array($params)) {
return $this->call($method, $params);
} else {
return $this->call($method, array($params));
}
}
/**
* @return string
*/
public function getHost() {
return $this->host;
}
/**
* @param string $newhost
* @return JSONAPI This object, for method chaining
*/
public function setHost($host) {
$this->host = $host;
return $this;
}
/**
* @return integer
*/
public function getPort() {
return $this->port;
}
/**
* @param integer $newport
* @return JSONAPI This object, for method chaining
*/
public function setPort($port) {
$port = (int) $port;
if ($port < 1 || $port > 65535) {
throw new Exception('The port must be between 1 and 65535, you supplied ' . $port);
}
$this->port = $port;
return $this;
}
/**
* @return string
*/
public function getUsername() {
return $this->username;
}
/**
* @param string $newusername
* @return JSONAPI This object, for method chaining
*/
public function setUsername($username) {
$this->username = $username;
return $this;
}
/**
* @return string
*/
public function getPassword() {
return $this->password;
}
/**
* @param string $newpassword
* @return JSONAPI This object, for method chaining
*/
public function setPassword($username) {
$this->password = $password;
return $this;
}
/**
* @return integer
*/
public function getTimeout() {
return $this->timeout;
}
/**
* @param integer $newtimeout
* @return JSONAPI This object, for method chaining
*/
public function setTimeout($timeout) {
$this->timeout = (int) $timeout;
return $this;
}
}