1 | <?php |
||
33 | class Http |
||
34 | { |
||
35 | /** |
||
36 | * Http client. |
||
37 | * |
||
38 | * @var HttpClient |
||
39 | */ |
||
40 | protected $client; |
||
41 | |||
42 | /** |
||
43 | * The middlewares. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $middlewares = []; |
||
48 | |||
49 | /** |
||
50 | * Guzzle client default settings. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected static $defaults = []; |
||
55 | |||
56 | /** |
||
57 | * Set guzzle default settings. |
||
58 | * |
||
59 | * @param array $defaults |
||
60 | */ |
||
61 | 4 | public static function setDefaultOptions($defaults = []) |
|
65 | |||
66 | /** |
||
67 | * Return current guzzle default settings. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | 1 | public static function getDefaultOptions() |
|
75 | |||
76 | /** |
||
77 | * GET request. |
||
78 | * |
||
79 | * @param string $url |
||
80 | * @param array $options |
||
81 | * |
||
82 | * @return array|bool |
||
83 | * |
||
84 | * @throws HttpException |
||
85 | */ |
||
86 | 1 | public function get($url, array $options = []) |
|
90 | |||
91 | /** |
||
92 | * POST request. |
||
93 | * |
||
94 | * @param string $url |
||
95 | * @param array|string $options |
||
96 | * |
||
97 | * @return array|bool |
||
98 | * |
||
99 | * @throws HttpException |
||
100 | */ |
||
101 | 1 | public function post($url, $options = []) |
|
107 | |||
108 | /** |
||
109 | * JSON request. |
||
110 | * |
||
111 | * @param string $url |
||
112 | * @param string|array $options |
||
113 | * @param int $encodeOption |
||
114 | * |
||
115 | * @return array|bool |
||
116 | * |
||
117 | * @throws HttpException |
||
118 | */ |
||
119 | 1 | public function json($url, $options = [], $encodeOption = JSON_UNESCAPED_UNICODE) |
|
125 | |||
126 | /** |
||
127 | * Upload file. |
||
128 | * |
||
129 | * @param string $url |
||
130 | * @param array $files |
||
131 | * @param array $form |
||
132 | * |
||
133 | * @return array|bool |
||
134 | * |
||
135 | * @throws HttpException |
||
136 | */ |
||
137 | 1 | public function upload($url, array $files = [], array $form = [], array $queries = []) |
|
154 | |||
155 | /** |
||
156 | * Set GuzzleHttp\Client. |
||
157 | * |
||
158 | * @param \GuzzleHttp\Client $client |
||
159 | * |
||
160 | * @return Http |
||
161 | */ |
||
162 | 6 | public function setClient(HttpClient $client) |
|
168 | |||
169 | /** |
||
170 | * Return GuzzleHttp\Client instance. |
||
171 | * |
||
172 | * @return \GuzzleHttp\Client. |
||
173 | */ |
||
174 | 3 | public function getClient() |
|
182 | |||
183 | /** |
||
184 | * Add a middleware. |
||
185 | * |
||
186 | * @param callable $middleware |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | 7 | public function addMiddleware(callable $middleware) |
|
196 | |||
197 | /** |
||
198 | * Return all middlewares. |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | 7 | public function getMiddlewares() |
|
206 | |||
207 | /** |
||
208 | * Make a request. |
||
209 | * |
||
210 | * @param string $url |
||
211 | * @param string $method |
||
212 | * @param array $options |
||
213 | * |
||
214 | * @return array|bool |
||
215 | * |
||
216 | * @throws HttpException |
||
217 | */ |
||
218 | 2 | public function request($url, $method = 'GET', $options = []) |
|
239 | |||
240 | /** |
||
241 | * @param \Psr\Http\Message\ResponseInterface|string $body |
||
242 | * |
||
243 | * @return mixed |
||
244 | * |
||
245 | * @throws \EasyWeChat\Core\Exceptions\HttpException |
||
246 | */ |
||
247 | 9 | public function parseJSON($body) |
|
248 | { |
||
249 | 9 | if ($body instanceof ResponseInterface) { |
|
250 | 2 | $body = $body->getBody(); |
|
251 | 2 | } |
|
252 | |||
253 | // XXX: json maybe contains special chars. So, let's FUCK the WeChat API developers ... |
||
254 | 9 | $body = $this->fuckTheWeChatInvalidJSON($body); |
|
255 | |||
256 | 9 | if (empty($body)) { |
|
257 | 1 | return false; |
|
258 | } |
||
259 | |||
260 | 9 | $contents = json_decode($body, true); |
|
261 | |||
262 | 9 | Log::debug('API response decoded:', compact('contents')); |
|
263 | |||
264 | 9 | if (JSON_ERROR_NONE !== json_last_error()) { |
|
265 | 1 | throw new HttpException('Failed to parse JSON: '.json_last_error_msg()); |
|
266 | } |
||
267 | |||
268 | 9 | return $contents; |
|
269 | } |
||
270 | |||
271 | /** |
||
272 | * Filter the invalid JSON string. |
||
273 | * |
||
274 | * @param \Psr\Http\Message\StreamInterface|string $invalidJSON |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 9 | protected function fuckTheWeChatInvalidJSON($invalidJSON) |
|
282 | |||
283 | /** |
||
284 | * Build a handler. |
||
285 | * |
||
286 | * @return HandlerStack |
||
287 | */ |
||
288 | 2 | protected function getHandler() |
|
298 | } |
||
299 |