Robot.php
5.09 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
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2016/12/7
* Time: 16:12
*/
namespace Hanson\Robot;
use Endroid\QrCode\QrCode;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class Robot
{
private $client;
public $tmpPath;
private $uuid;
private $baseUri = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin';
private $baseHost = 'wx2.qq.com';
private $redirectUri;
private $skey;
private $sid;
private $uin;
private $passTicket;
private $deviceId;
private $baseRequest;
public function __construct(Array $option = [])
{
$this->client = new Client();
$this->handleOption($option);
}
/**
* 处理option
*
* @param $option
*/
private function handleOption($option)
{
$this->tmpPath = $option['tmp'] ?? sys_get_temp_dir();
}
public function run()
{
$this->getUuid();
$this->generateQrCode();
$this->log('[INFO] 请用微信扫码');
if($this->waitForLogin() !== 200){
die('[ERROR] 微信登录失败');
}
if(!$this->login()){
die('[ERROR] 登录失败');
}
$this->log('[INFO] 登录成功');
$this->init();
}
/**
* 获取UUID
* @throws \Exception
*/
public function getUuid()
{
$content = $this->client->get('https://login.weixin.qq.com/jslogin', [
'query' => [
'appid' => 'wx782c26e4c19acffb',
'fun' => 'new',
'lang' => 'zh_CN',
'_' => time() * 1000 . random_int(1, 999)
]
])->getBody()->getContents();
preg_match('/window.QRLogin.code = (\d+); window.QRLogin.uuid = \"(\S+?)\"/', $content, $matches);
if(!$matches){
throw new \Exception('获取UUID失败');
}
$this->uuid = $matches[2];
}
/**
* 生成登录二维码
*/
public function generateQrCode()
{
$url = 'https://login.weixin.qq.com/l/' . $this->uuid;
$qrCode = new QrCode($url);
$file = $this->tmpPath . 'login_qr_code.png';
$qrCode->save($file);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
system($file);
}
}
/**
* 等待登录
*
* @return int
*/
public function waitForLogin(): int
{
$urlTemplate = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s';
$retryTime = 10;
$tip = 1;
$code = 0;
while($retryTime > 0){
$url = sprintf($urlTemplate, $tip, $this->uuid, time());
$content = $this->client->get($url)->getBody()->getContents();
preg_match('/window.code=(\d+);/', $content, $matches);
$code = $matches[1];
switch($code){
case '201':
$this->log('[INFO] 请点击确认进行登录');
$tip = 0;
break;
case '200':
preg_match('/window.redirect_uri="(\S+?)";/', $content, $matches);
$this->redirectUri = $matches[1] . '&fun=new';
return $code;
case '408':
$this->log('[ERROR] 微信登录超时。将在 1 秒后重试');
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
default:
$this->log('[ERROR] 微信登录异常。异常码:%s 。1 秒后重试', $code);
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
}
}
return $code;
}
public function login()
{
$content = $this->client->get($this->redirectUri)->getBody()->getContents();
$crawler = new Crawler($content);
$this->skey = $crawler->filter('error skey')->text();
$this->sid = $crawler->filter('error wxsid')->text();
$this->uin = $crawler->filter('error wxuin')->text();
$this->passTicket = $crawler->filter('error pass_ticket')->text();
if(in_array('', [$this->skey, $this->sid, $this->uin, $this->passTicket])){
return false;
}
$this->deviceId = 'e' . strval(random_int(100000000000000, 999999999999999));
$this->baseRequest = [
'Uin' => $this->uin,
'Sid' => $this->sid,
'Skey' => $this->skey,
'DeviceID' => $this->deviceId
];
return true;
}
public function init()
{
$url = sprintf($this->baseUri . '/webwxinit?r=%i&lang=en_US&pass_ticket=%s', time(), $this->passTicket);
$content = $this->client->post($url, [
'query' => [
'BaseRequest' => json_encode($this->baseRequest)
]
])->getBody()->getContents();
print_r($content);
}
private function log($msg, ...$args)
{
echo sprintf($msg . PHP_EOL, $args);
}
public function __get($value)
{
return $this->$value;
}
}