-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.php
More file actions
executable file
·256 lines (222 loc) · 5.61 KB
/
Http.php
File metadata and controls
executable file
·256 lines (222 loc) · 5.61 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
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
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* Http.php.
*
* This file is part of the wechat-components.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Core;
use EasyWeChat\Core\Exceptions\HttpException;
use EasyWeChat\Support\Log;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
/**
* Class Http.
*/
class Http
{
/**
* Http client.
*
* @var HttpClient
*/
protected $client;
/**
* The middlewares.
*
* @var array
*/
protected $middlewares = [];
/**
* GET request.
*
* @param string $url
* @param array $options
*
* @return array|bool
*
* @throws HttpException
*/
public function get($url, array $options = [])
{
return $this->request($url, 'GET', ['query' => $options]);
}
/**
* POST request.
*
* @param string $url
* @param array|string $options
*
* @return array|bool
*
* @throws HttpException
*/
public function post($url, $options = [])
{
return $this->request($url, 'POST', ['form_options' => $options]);
}
/**
* JSON request.
*
* @param string $url
* @param array $options
* @param int $encodeOption
*
* @return array|bool
*
* @throws HttpException
*/
public function json($url, array $options = [], $encodeOption = JSON_UNESCAPED_UNICODE)
{
$params = ['body' => json_encode($options, $encodeOption)];
return $this->request($url, 'POST', $params);
}
/**
* Upload file.
*
* @param string $url
* @param array $files
* @param array $form
*
* @return array|bool
*
* @throws HttpException
*/
public function upload($url, array $files = [], array $form = [], array $queries = [])
{
$multipart = [];
foreach ($files as $name => $path) {
$multipart[] = [
'name' => $name,
'contents' => fopen($path, 'r'),
];
}
foreach ($form as $name => $contents) {
$multipart[] = compact('name', 'contents');
}
return $this->request($url, 'POST', ['query' => $queries, 'multipart' => $multipart]);
}
/**
* Set GuzzleHttp\Client.
*
* @param \GuzzleHttp\Client $client
*
* @return Http
*/
public function setClient(HttpClient $client)
{
$this->client = $client;
return $this;
}
/**
* Return GuzzleHttp\Client instance.
*
* @return \GuzzleHttp\Client.
*/
public function getClient()
{
if (!($this->client instanceof HttpClient)) {
$this->client = new HttpClient();
}
return $this->client;
}
/**
* Add a middleware.
*
* @param callable $middleware
*
* @return $this
*/
public function addMiddleware(callable $middleware)
{
array_push($this->middlewares, $middleware);
return $this;
}
/**
* Return all middlewares.
*
* @return array
*/
public function getMiddlewares()
{
return $this->middlewares;
}
/**
* Make a request.
*
* @param string $url
* @param string $method
* @param array $options
*
* @return array|bool
*
* @throws HttpException
*/
public function request($url, $method = 'GET', $options = [])
{
$method = strtoupper($method);
Log::debug('Client Request:', compact('url', 'method', 'options'));
$options['handler'] = $this->getHandler();
$response = $this->getClient()->request($method, $url, $options);
Log::debug('API response:', [
'Status' => $response->getStatusCode(),
'Reason' => $response->getReasonPhrase(),
'Headers' => $response->getHeaders(),
'Body' => strval($response->getBody()),
]);
return $response;
}
/**
* @param \GuzzleHttp\Psr7\Stream|string $body
* @param bool|true $throws
*
* @return mixed
*
* @throws \EasyWeChat\Core\Exceptions\FaultException
* @throws \EasyWeChat\Core\Exceptions\HttpException
*/
public function parseJSON($body, $throws = true)
{
// XXX: json maybe contains special chars. So, let's FUCK the WeChat API developers ...
// $contents = json_decode(substr(str_replace(['\"', '\\\\'], ['"', ''], json_encode($body)), 1, -1), true);
if ($body instanceof ResponseInterface) {
$body = $body->getBody();
}
$contents = json_decode($body, true);
Log::debug('API response decoded:', compact('contents'));
if (JSON_ERROR_NONE !== json_last_error()) {
throw new HttpException('Failed to parse JSON.', json_last_error());
}
return $contents;
}
/**
* Build a handler.
*
* @return HandlerStack
*/
protected function getHandler()
{
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
foreach ($this->middlewares as $middleware) {
$stack->push($middleware);
}
return $stack;
}
}