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\Http\Response; |
16
|
|
|
use JinWeChat\Kernel\Traits\HasHttpRequests; |
17
|
|
|
use Psr\Http\Message\RequestInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class BaseClient. |
21
|
|
|
* |
22
|
|
|
* @author docodeit <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class BaseClient |
25
|
|
|
{ |
26
|
|
|
use HasHttpRequests { |
27
|
|
|
request as performRequest; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \JinWeChat\Kernel\ServiceContainer |
32
|
|
|
*/ |
33
|
|
|
protected $app; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \JinWeChat\Kernel\Contracts\CookiesInterface |
37
|
|
|
*/ |
38
|
|
|
protected $cookies; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $referrer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $token; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var |
52
|
|
|
*/ |
53
|
|
|
protected $baseUri; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* BaseClient constructor. |
57
|
|
|
* |
58
|
|
|
* @param \JinWeChat\Kernel\ServiceContainer $app |
59
|
|
|
*/ |
60
|
|
|
public function __construct(ServiceContainer $app) |
61
|
|
|
{ |
62
|
|
|
$this->app = $app; |
63
|
|
|
$this->referrer = $app['config']->get('http.base_uri', 'https://mp.weixin.qq.com/'); |
64
|
|
|
$this->token = isset($this->app->getConfig()['cookies']['token']) ? $this->app->getConfig()['cookies']['token'] : ''; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* GET request. |
69
|
|
|
* |
70
|
|
|
* @param string $url |
71
|
|
|
* @param array $query |
72
|
|
|
* |
73
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
74
|
|
|
*/ |
75
|
|
|
public function httpGet(string $url, array $query = []) |
76
|
|
|
{ |
77
|
|
|
return $this->request($url, 'GET', $query); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* POST request. |
82
|
|
|
* |
83
|
|
|
* @param string $url |
84
|
|
|
* @param array $data |
85
|
|
|
* |
86
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
87
|
|
|
*/ |
88
|
|
|
public function httpPost(string $url, array $data = []) |
89
|
|
|
{ |
90
|
|
|
return $this->request($url, 'POST', ['form_params' => $data]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* JSON request. |
95
|
|
|
* |
96
|
|
|
* @param string $url |
97
|
|
|
* @param string|array $data |
98
|
|
|
* @param array $query |
99
|
|
|
* |
100
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
101
|
|
|
*/ |
102
|
|
|
public function httpPostJson(string $url, array $data = [], array $query = []) |
103
|
|
|
{ |
104
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'json' => $data]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Upload file. |
109
|
|
|
* |
110
|
|
|
* @param string $url |
111
|
|
|
* @param array $files |
112
|
|
|
* @param array $form |
113
|
|
|
* @param array $query |
114
|
|
|
* |
115
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
116
|
|
|
*/ |
117
|
|
|
public function httpUpload(string $url, array $files = [], array $form = [], array $query = []) |
118
|
|
|
{ |
119
|
|
|
$multipart = []; |
120
|
|
|
|
121
|
|
|
foreach ($files as $name => $path) { |
122
|
|
|
$multipart[] = [ |
123
|
|
|
'name' => $name, |
124
|
|
|
'contents' => fopen($path, 'r'), |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
foreach ($form as $name => $contents) { |
129
|
|
|
$multipart[] = compact('name', 'contents'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $url |
137
|
|
|
* @param string $method |
138
|
|
|
* @param array $options |
139
|
|
|
* @param bool $returnRaw |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function request(string $url, string $method = 'GET', array $options = [], $returnRaw = false) |
144
|
|
|
{ |
145
|
|
|
if (empty($this->middlewares)) { |
146
|
|
|
$this->registerHttpMiddlewares(); |
147
|
|
|
} |
148
|
|
|
//options 加入token |
149
|
|
|
if (isset($options['query'])) { |
150
|
|
|
$options['query'] = array_merge($options['query'], ['token' => $this->token]); |
151
|
|
|
} |
152
|
|
|
$response = $this->performRequest($url, $method, $options); |
153
|
|
|
//保存Token todo::改为缓存 |
154
|
|
|
if (preg_match('/cgi-bin\/bizlogin?action=startlogin/', $url, $urlMatch)) { |
155
|
|
|
if (preg_match('/token=([\d]+)/i', $response['redirect_url'], $match)) { |
156
|
|
|
$this->token = $match[1]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $returnRaw ? $response : $response->getBody()->getContents(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $url |
165
|
|
|
* @param string $method |
166
|
|
|
* @param array $options |
167
|
|
|
* |
168
|
|
|
* @return \JinWeChat\Kernel\Http\Response |
169
|
|
|
*/ |
170
|
|
|
public function requestRaw(string $url, string $method = 'GET', array $options = []) |
171
|
|
|
{ |
172
|
|
|
return Response::buildFromPsrResponse($this->request($url, $method, $options, true)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return GuzzleHttp\Client instance. |
177
|
|
|
* |
178
|
|
|
* @return \GuzzleHttp\Client |
179
|
|
|
*/ |
180
|
|
|
public function getHttpClient(): Client |
181
|
|
|
{ |
182
|
|
|
if (!($this->httpClient instanceof Client)) { |
183
|
|
|
$this->httpClient = $this->app['http_client'] ?? new Client(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->httpClient; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Register Guzzle middlewares. |
191
|
|
|
*/ |
192
|
|
|
protected function registerHttpMiddlewares() |
193
|
|
|
{ |
194
|
|
|
//referrer |
195
|
|
|
$this->pushMiddleware($this->headerMiddleware('Referer', $this->referrer), 'referrer'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* apply referrer to the request header. |
200
|
|
|
* |
201
|
|
|
* @param $header |
202
|
|
|
* @param $value |
203
|
|
|
* |
204
|
|
|
* @return \Closure |
205
|
|
|
*/ |
206
|
|
|
public function headerMiddleware($header, $value) |
207
|
|
|
{ |
208
|
|
|
return function (callable $handler) use ($header, $value) { |
209
|
|
|
return function ( |
210
|
|
|
RequestInterface $request, |
211
|
|
|
array $options |
212
|
|
|
) use ( |
213
|
|
|
$handler, |
214
|
|
|
$header, |
215
|
|
|
$value |
216
|
|
|
) { |
217
|
|
|
$request = $request->withHeader($header, $value); |
218
|
|
|
|
219
|
|
|
return $handler($request, $options); |
220
|
|
|
}; |
221
|
|
|
}; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* 保存Cookies todo::保存到Cache. |
226
|
|
|
*/ |
227
|
|
|
public function saveCookies() |
228
|
|
|
{ |
229
|
|
|
$cookies = $this->app['http_client']->getConfig()['cookies']; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* 获取微秒. |
234
|
|
|
* |
235
|
|
|
* @return float |
236
|
|
|
*/ |
237
|
|
|
public function getMillisecond() |
238
|
|
|
{ |
239
|
|
|
list($s1, $s2) = explode(' ', microtime()); |
240
|
|
|
|
241
|
|
|
return (float) sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* 转换json. |
246
|
|
|
* |
247
|
|
|
* @param $json |
248
|
|
|
* |
249
|
|
|
* @return bool|string |
250
|
|
|
*/ |
251
|
|
|
public function jsonDecode($json) |
252
|
|
|
{ |
253
|
|
|
$res = json_decode($json); |
254
|
|
|
if (JSON_ERROR_NONE === json_last_error()) { |
255
|
|
|
return $res; |
256
|
|
|
} else { |
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.