1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
7
|
|
|
use GuzzleHttp\RequestOptions; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Tgallice\FBMessenger\Exception\ApiException; |
10
|
|
|
|
11
|
|
|
class Client |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* API base uri |
15
|
|
|
*/ |
16
|
|
|
const API_BASE_URI = 'https://graph.facebook.com/'; |
17
|
|
|
|
18
|
|
|
/* |
19
|
|
|
* API Version |
20
|
|
|
*/ |
21
|
|
|
const DEFAULT_API_VERSION = 'v2.6'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Request default timeout |
25
|
|
|
*/ |
26
|
|
|
const DEFAULT_TIMEOUT = 60; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Request default file upload timeout |
30
|
|
|
*/ |
31
|
|
|
const DEFAULT_FILE_UPLOAD_TIMEOUT = 3600; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
public static $allowedMethod = ['POST', 'GET', 'PUT', 'DELETE']; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string Wit app token |
40
|
|
|
*/ |
41
|
|
|
private $accessToken; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ClientInterface client |
45
|
|
|
*/ |
46
|
|
|
private $client; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ResponseInterface|null |
50
|
|
|
*/ |
51
|
|
|
private $lastResponse; |
52
|
|
|
|
53
|
15 |
|
public function __construct($accessToken, ClientInterface $httpClient = null) |
54
|
|
|
{ |
55
|
15 |
|
$this->accessToken = $accessToken; |
56
|
15 |
|
$this->client = $httpClient ?: $this->defaultHttpClient(); |
57
|
15 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $uri |
61
|
|
|
* @param mixed $body |
62
|
|
|
* |
63
|
|
|
* @return ResponseInterface |
64
|
|
|
*/ |
65
|
2 |
|
public function post($uri, $body = null) |
66
|
|
|
{ |
67
|
2 |
|
return $this->send('POST', $uri, $body); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $uri |
72
|
|
|
* @param array $params |
73
|
|
|
* |
74
|
|
|
* @return ResponseInterface |
75
|
|
|
*/ |
76
|
1 |
|
public function get($uri, array $params = []) |
77
|
|
|
{ |
78
|
1 |
|
return $this->send('GET', $uri, null, $params); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $uri |
83
|
|
|
* @param mixed $data |
84
|
|
|
* |
85
|
|
|
* @return ResponseInterface |
86
|
|
|
*/ |
87
|
1 |
|
public function put($uri, $data = null) |
88
|
|
|
{ |
89
|
1 |
|
return $this->send('PUT', $uri, $data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $uri |
94
|
|
|
* |
95
|
|
|
* @return ResponseInterface |
96
|
|
|
*/ |
97
|
1 |
|
public function delete($uri) |
98
|
|
|
{ |
99
|
1 |
|
return $this->send('DELETE', $uri); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $method |
104
|
|
|
* @param string $uri |
105
|
|
|
* @param mixed $body |
106
|
|
|
* @param array $query |
107
|
|
|
* @param array $headers |
108
|
|
|
* @param array $options |
109
|
|
|
* |
110
|
|
|
* @return ResponseInterface |
111
|
|
|
* |
112
|
|
|
* @throws ApiException |
113
|
|
|
*/ |
114
|
13 |
|
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []) |
115
|
|
|
{ |
116
|
13 |
|
$options = $this->enhanceOptions($body, $query, $headers, $options); |
117
|
|
|
|
118
|
|
|
try { |
119
|
13 |
|
$this->lastResponse = $this->client->request($method, $uri, $options); |
120
|
13 |
|
} catch (GuzzleException $e) { |
121
|
1 |
|
throw new ApiException($e->getMessage(), $e->getCode()); |
122
|
|
|
} |
123
|
|
|
|
124
|
12 |
|
$this->validateResponse($this->lastResponse); |
125
|
|
|
|
126
|
11 |
|
return $this->lastResponse; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the last response from the API |
131
|
|
|
* |
132
|
|
|
* @return null|ResponseInterface |
133
|
|
|
*/ |
134
|
1 |
|
public function getLastResponse() |
135
|
|
|
{ |
136
|
1 |
|
return $this->lastResponse; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function getHttpClient() |
140
|
|
|
{ |
141
|
1 |
|
return $this->client; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ResponseInterface $response |
146
|
|
|
* |
147
|
|
|
* @throws ApiException |
148
|
|
|
*/ |
149
|
12 |
|
private function validateResponse(ResponseInterface $response) |
150
|
|
|
{ |
151
|
12 |
|
if ($response->getStatusCode() !== 200) { |
152
|
1 |
|
$responseData = json_decode((string) $response->getBody(), true); |
153
|
1 |
|
$code = isset($responseData['error']['code']) ? $responseData['error']['code'] : 0; |
154
|
1 |
|
$message = isset($responseData['error']['message']) ? $responseData['error']['message'] : $response->getReasonPhrase(); |
155
|
|
|
|
156
|
1 |
|
throw new ApiException($message, $code, $responseData); |
157
|
|
|
} |
158
|
11 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return ClientInterface |
162
|
|
|
*/ |
163
|
1 |
|
private function defaultHttpClient() |
164
|
|
|
{ |
165
|
1 |
|
return new \GuzzleHttp\Client([ |
166
|
1 |
|
'base_uri' => self::API_BASE_URI . self::DEFAULT_API_VERSION, |
167
|
1 |
|
'timeout' => self::DEFAULT_TIMEOUT, |
168
|
1 |
|
'connect_timeout' => 10, |
169
|
1 |
|
]); |
170
|
|
|
} |
171
|
|
|
|
172
|
13 |
|
private function enhanceOptions($body = null, array $query = [], array $headers = [], array $options = []) |
173
|
|
|
{ |
174
|
|
|
// Add token |
175
|
13 |
|
if (!isset($query['access_token'])) { |
176
|
13 |
|
$query['access_token'] = $this->accessToken; |
177
|
13 |
|
} |
178
|
|
|
|
179
|
13 |
|
if (!empty($body) && is_array($body)) { |
180
|
|
|
// Encode and set the content type |
181
|
2 |
|
$body = json_encode($body); |
182
|
2 |
|
$headers['Content-Type'] = 'application/json'; |
183
|
2 |
|
} |
184
|
|
|
|
185
|
13 |
|
$options[RequestOptions::BODY] = $body; |
186
|
13 |
|
$options[RequestOptions::QUERY] = $query; |
187
|
13 |
|
$options[RequestOptions::HEADERS] = $headers; |
188
|
|
|
|
189
|
13 |
|
return $options; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|