-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenApi.php
385 lines (326 loc) · 9.07 KB
/
OpenApi.php
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
require_once 'vendor/autoload.php';
use phpseclib\Crypt\RSA;
// Creates the API Request from the context
class APIRequest {
var $context;
// Constructer context
function __construct($context) {
if ($context != null && get_class($context) != 'APIContext') {
throw new Exception('Input must be an APIContext');
}
$this->context = $context;
}
// Does the HTTP Request
function execute() {
try {
if ($this->context == null) {
throw new Exception('Context cannot be null');
}
$this->create_default_headers();
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
switch ($this->context->get_method_type()) {
case APIMethodType::GET:
return $this->__get($ch);
case APIMethodType::POST:
return $this->__post($ch);
case APIMethodType::PUT:
return $this->__put($ch);
default:
return null;
}
} catch (\Exception $e) {
echo "Error from php api: " . $e->getMessage() . '<br>';
}
}
/**
* @from phpseclib RSA
* Creates the Authorisation bearer token using the RSA public key provided from Crypt_RSA
*
*/
function create_bearer_token() {
// Need to do these lines to create a 'valid' formatted RSA key for the openssl library
$rsa = new RSA();
$rsa->loadKey($this->context->get_public_key());
$rsa->setPublicKey($this->context->get_public_key());
$publickey = $rsa->getPublicKey();
$api_encrypted = '';
$encrypted = '';
if (openssl_public_encrypt($this->context->get_api_key(), $encrypted, $publickey)) {
$api_encrypted = base64_encode($encrypted);
}
return $api_encrypted;
}
// Add the default headers
function create_default_headers() {
$this->context->add_header('Authorization', 'Bearer ' . $this->create_bearer_token());
$this->context->add_header('Content-Type','application/json');
$this->context->add_header('Host', $this->context->get_address());
}
// Do a GET request
function __get($ch) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, $this->context->get_url().'?'.http_build_query($this->context->get_parameters()));
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->context->get_headers());
$response = curl_exec($ch);
echo $response;
echo '<br>';
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($ch);
return new APIResponse($status_code, $headers, $body);
}
// Do a POST request
function __post($ch) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $this->context->get_url());
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->context->get_headers());
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->context->get_parameters()));
$response = curl_exec($ch);
echo $response;
echo '<br>';
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($ch);
return new APIResponse($status_code, $headers, $body);
}
// Do a PUT request
function __put($ch) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_URL, $this->context->get_url());
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->context->get_headers());
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->context->get_parameters()));
$response = curl_exec($ch);
echo $response;
echo '<br>';
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($ch);
return new APIResponse($status_code, $headers, $body);
}
function __unknown() {
throw new Exception('Unknown method');
}
}
// API Response
class APIResponse {
var $status_code;
var $headers;
var $body;
// Constructer
function __construct($status_code, $headers, $body) {
$this->set_status_code($status_code);
$this->set_headers($headers);
$this->set_body($body);
}
function get_status_code() {
return $this->status_code;
}
function set_status_code($status_code) {
if (gettype($status_code) != 'integer') {
throw new Exception('status_code must be a integer');
} else {
$this->status_code = $status_code;
}
}
function get_headers() {
return $this->headers;
}
function set_headers($headers) {
if (gettype($headers) != 'string') {
throw new Exception('headers must be a string');
} else {
$this->headers = $headers;
}
}
function get_body() {
return $this->body;
}
function set_body($body) {
if (gettype($body) != 'string') {
throw new Exception('body must be a string');
} else {
$this->body = $body;
}
}
}
// Api Method Type Constants
class APIMethodType {
const GET = 0;
const POST = 1;
const PUT = 2;
const DELETE = 4;
}
// API Context that contain info for the API endpoint
class APIContext {
var $api_key = '';
var $public_key = '';
var $ssl = false;
var $method_type = APIMethodType::GET;
var $address = '';
var $port = 80;
var $path = '';
var $headers = array();
var $parameters = array();
// Constructor with optional prepopulated variables
function __construct($dictionary=null)
{
if ($dictionary != null && gettype($dictionary) != 'array') {
throw new Exception('Input must be an array');
}
if ($dictionary != null) {
foreach($dictionary as $i => $item) {
switch (strtolower($i)) {
case 'api_key':
$this->set_api_key($dictionary[$i]);
break;
case 'public_key':
$this->set_public_key($dictionary[$i]);
break;
case 'ssl':
$this->set_ssl($dictionary[$i]);
break;
case 'method_type':
$this->set_method_type($dictionary[$i]);
break;
case 'address':
$this->set_address($dictionary[$i]);
break;
case 'port':
$this->set_port($dictionary[$i]);
break;
case 'path':
$this->set_path($dictionary[$i]);
break;
case 'headers':
if (gettype($dictionary[$i]) != 'array') {
throw new Exception('headers must be an array');
}
foreach($dictionary[$i] as $key => $value) {
$this->add_header($key, $dictionary[$i][$key]);
}
break;
case 'parameters':
if (gettype($dictionary[$i]) != 'array') {
throw new Exception('parameters must be an array');
}
foreach($dictionary[$i] as $key => $value) {
$this->add_parameter($key, $dictionary[$i][$key]);
}
break;
default:
echo 'Unknown parameter type';
}
}
}
}
// Build the URL from context parameters
function get_url() {
if ($this->ssl == true) {
return 'https://' . $this->address . ':' . $this->port . $this->path;
} else {
return 'http://' . $this->address . ':' . $this->port . $this->path;
}
}
// Add/update headers
function add_header($header, $value) {
$this->headers[$header] = $value;
}
// Get headers as an array
function get_headers() {
$headers = array();
foreach($this->headers as $key => $value) {
array_push($headers, $key . ": " . $value);
}
return $headers;
}
// Add parameter
function add_parameter($key, $value) {
$this->parameters[$key] = $value;
}
// Get parameters
function get_parameters() {
return $this->parameters;
}
function get_api_key() {
return $this->api_key;
}
function set_api_key($api_key) {
if (gettype($api_key) != 'string') {
throw new Exception('api_key must be a string');
} else {
$this->api_key = $api_key;
}
}
function get_public_key() {
return $this->public_key;
}
function set_public_key($public_key) {
if (gettype($public_key) != 'string') {
throw new Exception('public_key must be a string');
} else {
$this->public_key = $public_key;
}
}
function get_ssl() {
return $this->ssl;
}
function set_ssl($ssl) {
if (gettype($ssl) != 'boolean') {
throw new Exception('ssl must be a boolean');
} else {
$this->ssl = $ssl;
}
}
function get_method_type() {
return $this->method_type;
}
function set_method_type($method_type) {
if (gettype($method_type) != 'integer') {
throw new Exception('method_type must be a integer');
} else {
$this->method_type = $method_type;
}
}
function get_address() {
return $this->address;
}
function set_address($address) {
if (gettype($address) != 'string') {
throw new Exception('address must be a string');
} else {
$this->address = $address;
}
}
function get_port() {
return $this->port;
}
function set_port($port) {
if ($port != null && gettype($port) != 'integer') {
throw new Exception('port must be a integer');
} else {
$this->port = $port;
}
}
function get_path() {
return $this->path;
}
function set_path($path) {
if (gettype($path) != 'string') {
throw new Exception('path must be a string');
} else {
$this->path = $path;
}
}
}
?>