-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceApi.php
More file actions
94 lines (87 loc) · 2.71 KB
/
ResourceApi.php
File metadata and controls
94 lines (87 loc) · 2.71 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
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
class ResourceApi
{
protected $accessToken = [];
protected static $instance = null;
protected $headers = [];
public function setHeaders($headers)
{
$this->headers = $headers;
}
public function getHeaders()
{
return $this->headers;
}
public function setParams($params)
{
$this->params = $params;
}
public function getParams()
{
return $this->params;
}
/*
* Requests can be created using various methods of a client.
* You can create and send requests using one of the following methods:
*
* GET: Sends a GET request
* HEAD: Sends a HEAD request
* POST: Sends a POST request
* PUT: Sends a PUT request
* DELETE: Sends a DELETE request
* OPTIONS: Sends an OPTIONS request
*
*/
public function createRequest($method, $api)
{
$client = new Client();
try {
$request = $client->createRequest(
strtoupper($method),
$api,
[
'auth' => [ 'pecel', 'lele' ],
$this->getParams()
]
);
$response = $client->send($request);
return $response->json();
}
catch (ClientException $e) {
// In the event of a networking error (connection timeout, DNS errors, etc.)
$response = $e->getResponse();
$body = $response->json();
$body['header'] = [
'code' => $response->getStatusCode(),
'message' => $response->getReasonPhrase(),
'description' => $response->json()
];
return $body;
}
catch (ClientException $e) {
// Thrown for 400 level errors
$response = $e->getResponse();
$body = $response->json();
$body['header'] = [
'code' => $response->getStatusCode(),
'message' => $response->getReasonPhrase(),
'description' => $response->json()
];
return $body;
}
catch (ServerException $e) {
// Thrown for 500 level errors
$response = $e->getResponse();
$body['header'] = [
'code' => $response->getStatusCode(),
'message' => $response->getReasonPhrase(),
'description' => $response->getBody()
];
return $body;
}
}
}