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
|
14 |
|
public function __construct($accessToken, ClientInterface $httpClient = null) |
54
|
|
|
{ |
55
|
14 |
|
$this->accessToken = $accessToken; |
56
|
14 |
|
$this->client = $httpClient ?: $this->defaultHttpClient(); |
57
|
14 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $uri |
61
|
|
|
* @param string|null $body |
62
|
|
|
* |
63
|
|
|
* @return ResponseInterface |
64
|
|
|
*/ |
65
|
1 |
|
public function post($uri, $body = null) |
66
|
|
|
{ |
67
|
1 |
|
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 null|string $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
|
12 |
|
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []) |
115
|
|
|
{ |
116
|
12 |
|
$query = $this->addToken($query); |
117
|
|
|
|
118
|
|
|
try { |
119
|
12 |
|
$options[RequestOptions::BODY] = $body; |
120
|
12 |
|
$options[RequestOptions::QUERY] = $query; |
121
|
12 |
|
$options[RequestOptions::HEADERS] = $headers; |
122
|
|
|
|
123
|
12 |
|
$this->lastResponse = $this->client->request($method, $uri, $options); |
124
|
12 |
|
} catch (GuzzleException $e) { |
125
|
1 |
|
throw new ApiException($e->getMessage(), $e->getCode()); |
126
|
|
|
} |
127
|
|
|
|
128
|
11 |
|
$this->validateResponse($this->lastResponse); |
129
|
|
|
|
130
|
10 |
|
return $this->lastResponse; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the last response from the API |
135
|
|
|
* |
136
|
|
|
* @return null|ResponseInterface |
137
|
|
|
*/ |
138
|
1 |
|
public function getLastResponse() |
139
|
|
|
{ |
140
|
1 |
|
return $this->lastResponse; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
public function getHttpClient() |
144
|
|
|
{ |
145
|
1 |
|
return $this->client; |
146
|
|
|
} |
147
|
|
|
|
148
|
12 |
|
private function addToken(array $query) |
149
|
|
|
{ |
150
|
12 |
|
$query['access_token'] = $this->accessToken; |
151
|
|
|
|
152
|
12 |
|
return $query; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param ResponseInterface $response |
157
|
|
|
* |
158
|
|
|
* @throws ApiException |
159
|
|
|
*/ |
160
|
11 |
|
private function validateResponse(ResponseInterface $response) |
161
|
|
|
{ |
162
|
11 |
|
if ($response->getStatusCode() !== 200) { |
163
|
1 |
|
$responseData = json_decode((string) $response->getBody(), true); |
164
|
1 |
|
$code = isset($responseData['error']['code']) ? $responseData['error']['code'] : 0; |
165
|
1 |
|
$message = isset($responseData['error']['message']) ? $responseData['error']['message'] : $response->getReasonPhrase(); |
166
|
|
|
|
167
|
1 |
|
throw new ApiException($message, $code, $responseData); |
168
|
|
|
} |
169
|
10 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return ClientInterface |
173
|
|
|
*/ |
174
|
1 |
|
private function defaultHttpClient() |
175
|
|
|
{ |
176
|
1 |
|
return new \GuzzleHttp\Client([ |
177
|
1 |
|
'base_uri' => self::API_BASE_URI . self::DEFAULT_API_VERSION, |
178
|
1 |
|
'timeout' => self::DEFAULT_TIMEOUT, |
179
|
1 |
|
'connect_timeout' => 10, |
180
|
1 |
|
]); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|