1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the docodeit/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) docodeit <[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 JinWeChat\Kernel; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use JinWeChat\Kernel\Contracts\CookiesInterface; |
16
|
|
|
use JinWeChat\Kernel\Http\Response; |
17
|
|
|
use JinWeChat\Kernel\Traits\HasHttpRequests; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class BaseClient. |
22
|
|
|
* |
23
|
|
|
* @author docodeit <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class BaseClient |
26
|
|
|
{ |
27
|
|
|
use HasHttpRequests { |
28
|
|
|
request as performRequest; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \JinWeChat\Kernel\ServiceContainer |
33
|
|
|
*/ |
34
|
|
|
protected $app; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \JinWeChat\Kernel\Contracts\CookiesInterface |
38
|
|
|
*/ |
39
|
|
|
protected $cookies; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $referrer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $token; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var |
53
|
|
|
*/ |
54
|
|
|
protected $baseUri; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* BaseClient constructor. |
58
|
|
|
* |
59
|
|
|
* @param \JinWeChat\Kernel\ServiceContainer $app |
60
|
|
|
* @param \JinWeChat\Kernel\Contracts\CookiesInterface|null $cookies |
61
|
|
|
*/ |
62
|
|
|
public function __construct(ServiceContainer $app, CookiesInterface $cookies = null) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->app = $app; |
65
|
|
|
$this->referrer = $app['config']->get('http.base_uri', 'https://mp.weixin.qq.com/'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* GET request. |
70
|
|
|
* |
71
|
|
|
* @param string $url |
72
|
|
|
* @param array $query |
73
|
|
|
* |
74
|
|
|
* @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException |
75
|
|
|
* |
76
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
77
|
|
|
*/ |
78
|
|
|
public function httpGet(string $url, array $query = []) |
79
|
|
|
{ |
80
|
|
|
return $this->request($url, 'GET', $query); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* POST request. |
85
|
|
|
* |
86
|
|
|
* @param string $url |
87
|
|
|
* @param array $data |
88
|
|
|
* |
89
|
|
|
* @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException |
90
|
|
|
* |
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
92
|
|
|
*/ |
93
|
|
|
public function httpPost(string $url, array $data = []) |
94
|
|
|
{ |
95
|
|
|
return $this->request($url, 'POST', ['form_params' => $data]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* JSON request. |
100
|
|
|
* |
101
|
|
|
* @param string $url |
102
|
|
|
* @param string|array $data |
103
|
|
|
* @param array $query |
104
|
|
|
* |
105
|
|
|
* @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException |
106
|
|
|
* |
107
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
108
|
|
|
*/ |
109
|
|
|
public function httpPostJson(string $url, array $data = [], array $query = []) |
110
|
|
|
{ |
111
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'json' => $data]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Upload file. |
116
|
|
|
* |
117
|
|
|
* @param string $url |
118
|
|
|
* @param array $files |
119
|
|
|
* @param array $form |
120
|
|
|
* @param array $query |
121
|
|
|
* |
122
|
|
|
* @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException |
123
|
|
|
* |
124
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
125
|
|
|
*/ |
126
|
|
|
public function httpUpload(string $url, array $files = [], array $form = [], array $query = []) |
127
|
|
|
{ |
128
|
|
|
$multipart = []; |
129
|
|
|
|
130
|
|
|
foreach ($files as $name => $path) { |
131
|
|
|
$multipart[] = [ |
132
|
|
|
'name' => $name, |
133
|
|
|
'contents' => fopen($path, 'r'), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($form as $name => $contents) { |
138
|
|
|
$multipart[] = compact('name', 'contents'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $url |
146
|
|
|
* @param string $method |
147
|
|
|
* @param array $options |
148
|
|
|
* @param bool $returnRaw |
149
|
|
|
* |
150
|
|
|
* @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException |
151
|
|
|
* |
152
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
153
|
|
|
*/ |
154
|
|
|
public function request(string $url, string $method = 'GET', array $options = [], $returnRaw = false) |
155
|
|
|
{ |
156
|
|
|
if (empty($this->middlewares)) { |
157
|
|
|
$this->registerHttpMiddlewares(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$response = $this->performRequest($url, $method, $options); |
161
|
|
|
//保存Token todo::改为缓存 |
162
|
|
|
if (preg_match('/cgi-bin\/bizlogin?action=startlogin/', $url, $urlMatch)) { |
163
|
|
|
if (preg_match('/token=([\d]+)/i', $response['redirect_url'], $match)) { |
164
|
|
|
$this->token = $match[1]; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $returnRaw ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $url |
173
|
|
|
* @param string $method |
174
|
|
|
* @param array $options |
175
|
|
|
* |
176
|
|
|
* @throws \JinWeChat\Kernel\Exceptions\InvalidConfigException |
177
|
|
|
* |
178
|
|
|
* @return \JinWeChat\Kernel\Http\Response |
179
|
|
|
*/ |
180
|
|
|
public function requestRaw(string $url, string $method = 'GET', array $options = []) |
181
|
|
|
{ |
182
|
|
|
return Response::buildFromPsrResponse($this->request($url, $method, $options, true)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return GuzzleHttp\Client instance. |
187
|
|
|
* |
188
|
|
|
* @return \GuzzleHttp\Client |
189
|
|
|
*/ |
190
|
|
|
public function getHttpClient(): Client |
191
|
|
|
{ |
192
|
|
|
if (!($this->httpClient instanceof Client)) { |
193
|
|
|
$this->httpClient = $this->app['http_client'] ?? new Client(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->httpClient; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Register Guzzle middlewares. |
201
|
|
|
*/ |
202
|
|
|
protected function registerHttpMiddlewares() |
203
|
|
|
{ |
204
|
|
|
//referrer |
205
|
|
|
$this->pushMiddleware($this->headerMiddleware('Referer', $this->referrer), 'referrer'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* apply referrer to the request header. |
210
|
|
|
* |
211
|
|
|
* @param $header |
212
|
|
|
* @param $value |
213
|
|
|
* |
214
|
|
|
* @return \Closure |
215
|
|
|
*/ |
216
|
|
|
public function headerMiddleware($header, $value) |
217
|
|
|
{ |
218
|
|
|
return function (callable $handler) use ($header, $value) { |
219
|
|
|
return function ( |
220
|
|
|
RequestInterface $request, |
221
|
|
|
array $options |
222
|
|
|
) use ( |
223
|
|
|
$handler, |
224
|
|
|
$header, |
225
|
|
|
$value |
226
|
|
|
) { |
227
|
|
|
$request = $request->withHeader($header, $value); |
228
|
|
|
|
229
|
|
|
return $handler($request, $options); |
230
|
|
|
}; |
231
|
|
|
}; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* 保存Cookies todo::保存到Cache |
236
|
|
|
*/ |
237
|
|
|
public function saveCookies() |
238
|
|
|
{ |
239
|
|
|
$cookies = $this->app['http_client']->getConfig()['cookies']; |
|
|
|
|
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.