|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyWeChat\Kernel\Traits; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
|
15
|
|
|
use GuzzleHttp\ClientInterface; |
|
16
|
|
|
use GuzzleHttp\HandlerStack; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Trait HasHttpRequests. |
|
21
|
|
|
* |
|
22
|
|
|
* @author overtrue <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
trait HasHttpRequests |
|
25
|
|
|
{ |
|
26
|
|
|
use ResponseCastable; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \GuzzleHttp\ClientInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $httpClient; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $middlewares = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \GuzzleHttp\HandlerStack |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $handlerStack; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static $defaults = [ |
|
47
|
|
|
'curl' => [ |
|
48
|
|
|
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, |
|
49
|
|
|
], |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set guzzle default settings. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $defaults |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public static function setDefaultOptions($defaults = []) |
|
58
|
|
|
{ |
|
59
|
1 |
|
self::$defaults = $defaults; |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return current guzzle default settings. |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public static function getDefaultOptions(): array |
|
68
|
|
|
{ |
|
69
|
1 |
|
return self::$defaults; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set GuzzleHttp\Client. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \GuzzleHttp\ClientInterface $httpClient |
|
76
|
|
|
* |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
38 |
|
public function setHttpClient(ClientInterface $httpClient) |
|
80
|
|
|
{ |
|
81
|
38 |
|
$this->httpClient = $httpClient; |
|
82
|
|
|
|
|
83
|
38 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return GuzzleHttp\ClientInterface instance. |
|
88
|
|
|
* |
|
89
|
|
|
* @return ClientInterface |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public function getHttpClient(): ClientInterface |
|
92
|
|
|
{ |
|
93
|
3 |
|
if (!($this->httpClient instanceof ClientInterface)) { |
|
|
|
|
|
|
94
|
1 |
|
if ($this->app['http_client']) { |
|
95
|
|
|
$this->httpClient = $this->app['http_client']; |
|
96
|
|
|
} else { |
|
97
|
3 |
|
$this->httpClient = new Client(['handler' => HandlerStack::create($this->getGuzzleHandler())]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->httpClient; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Add a middleware. |
|
106
|
|
|
* |
|
107
|
|
|
* @param callable $middleware |
|
108
|
3 |
|
* @param string|null $name |
|
109
|
|
|
* |
|
110
|
3 |
|
* @return $this |
|
111
|
3 |
|
*/ |
|
112
|
|
|
public function pushMiddleware(callable $middleware, string $name = null) |
|
113
|
1 |
|
{ |
|
114
|
|
|
if (!is_null($name)) { |
|
115
|
|
|
$this->middlewares[$name] = $middleware; |
|
116
|
3 |
|
} else { |
|
117
|
|
|
array_push($this->middlewares, $middleware); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
1 |
|
* Return all middlewares. |
|
125
|
|
|
* |
|
126
|
1 |
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getMiddlewares(): array |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->middlewares; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Make a request. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $url |
|
137
|
|
|
* @param string $method |
|
138
|
2 |
|
* @param array $options |
|
139
|
|
|
* |
|
140
|
2 |
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function request($url, $method = 'GET', $options = []): ResponseInterface |
|
143
|
|
|
{ |
|
144
|
2 |
|
$method = strtoupper($method); |
|
145
|
|
|
|
|
146
|
2 |
|
$options = array_merge(self::$defaults, $options, ['handler' => $this->getHandlerStack()]); |
|
147
|
2 |
|
|
|
148
|
|
|
$options = $this->fixJsonIssue($options); |
|
149
|
|
|
|
|
150
|
2 |
|
if (property_exists($this, 'baseUri') && !is_null($this->baseUri)) { |
|
151
|
2 |
|
$options['base_uri'] = $this->baseUri; |
|
152
|
|
|
} |
|
153
|
2 |
|
|
|
154
|
|
|
$response = $this->getHttpClient()->request($method, $url, $options); |
|
155
|
|
|
$response->getBody()->rewind(); |
|
156
|
|
|
|
|
157
|
|
|
return $response; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
1 |
|
* @param \GuzzleHttp\HandlerStack $handlerStack |
|
162
|
|
|
* |
|
163
|
1 |
|
* @return $this |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function setHandlerStack(HandlerStack $handlerStack) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->handlerStack = $handlerStack; |
|
168
|
|
|
|
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
1 |
|
* Build a handler stack. |
|
174
|
|
|
* |
|
175
|
1 |
|
* @return \GuzzleHttp\HandlerStack |
|
176
|
1 |
|
*/ |
|
177
|
|
|
public function getHandlerStack(): HandlerStack |
|
178
|
|
|
{ |
|
179
|
1 |
|
if ($this->handlerStack) { |
|
180
|
|
|
return $this->handlerStack; |
|
181
|
1 |
|
} |
|
182
|
1 |
|
|
|
183
|
|
|
$this->handlerStack = HandlerStack::create($this->getGuzzleHandler()); |
|
184
|
|
|
|
|
185
|
1 |
|
foreach ($this->middlewares as $name => $middleware) { |
|
186
|
|
|
$this->handlerStack->push($middleware, $name); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $this->handlerStack; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
2 |
|
* @param array $options |
|
194
|
|
|
* |
|
195
|
2 |
|
* @return array |
|
196
|
1 |
|
*/ |
|
197
|
|
|
protected function fixJsonIssue(array $options): array |
|
198
|
1 |
|
{ |
|
199
|
1 |
|
if (isset($options['json']) && is_array($options['json'])) { |
|
200
|
|
|
$options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json']); |
|
201
|
1 |
|
|
|
202
|
|
|
if (empty($options['json'])) { |
|
203
|
|
|
$options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT); |
|
204
|
1 |
|
} else { |
|
205
|
|
|
$options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_UNESCAPED_UNICODE); |
|
206
|
|
|
} |
|
207
|
2 |
|
|
|
208
|
|
|
unset($options['json']); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $options; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get guzzle handler. |
|
216
|
|
|
* @return callable |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function getGuzzleHandler() |
|
219
|
|
|
{ |
|
220
|
|
|
if (isset($this->app['guzzle_handler']) && is_string($this->app['guzzle_handler'])) { |
|
221
|
|
|
$handler = $this->app['guzzle_handler']; |
|
222
|
|
|
return new $handler(); |
|
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return \GuzzleHttp\choose_handler(); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|