-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXiaoIceAPI.php
More file actions
107 lines (88 loc) · 2.69 KB
/
XiaoIceAPI.php
File metadata and controls
107 lines (88 loc) · 2.69 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
<?php
use GuzzleHttp\Client as HttpClient;
use Symfony\Component\DomCrawler\Crawler;
class XiaoIceAPI
{
private $headers = [];
public function __construct($headers)
{
$this->initHeaders($headers);
}
private function initHeaders($headers)
{
foreach (explode(PHP_EOL, $headers) as $line) {
if (trim($line) == '') {
continue;
}
list($key, $value) = explode(':', $line, 2);
if (trim($key) == '') {
continue;
}
$this->headers[trim($key)] = $value;
}
unset($this->headers['Host']);
}
public function chat($query)
{
$data = [
'location' => 'msgdialog',
'module' => 'msgissue',
'style_id' => 1,
'text' => $query,
'uid' => 5175429989,
'tovfids' => '',
'fids' => '',
'el' => '[object HTMLDivElement]',
'_t' => 0,
];
$client = new HttpClient();
try {
$response = $client->post('https://www.weibo.com/aj/message/add?ajwvr=6', [
'form_params' => $data,
'headers' => $this->headers,
]);
$result = (string) $response->getBody();
$result = json_decode($result, true);
if ($result['code'] == '100000') {
return $this->dicts('success', $this->loop($query));
} else {
return $this->dicts('fail', $result['msg']);
}
} catch (Exception $e) {
return $this->dicts('fail', $e->getMessage());
}
}
private function loop($query)
{
$times = 1;
while ($times) {
$times++;
$client = new HttpClient();
$response = $client->get('https://www.weibo.com/aj/message/getbyid?ajwvr=6&uid=5175429989&count=1&_t=0', [
'headers' => $this->headers,
]);
$result = (string) $response->getBody();
$result = json_decode($result, true);
$html = $result['data']['html'];
$crawler = new Crawler();
$crawler->addHtmlContent($html);
$crawler = $crawler->filter('p.page');
$reply = '抓取小冰结果超时啦QwQ';
try {
$reply = $crawler->text();
} catch (Exception $e) {}
if ($reply != $query || $times > 20) {
break;
}
usleep(0.3 * 1000000);
}
return $reply;
}
private function dicts($status, $text)
{
return [
'status' => $status,
'text' => $text,
];
}
}