-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathYiiChina.php
133 lines (102 loc) · 3.43 KB
/
YiiChina.php
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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Curl\Curl;
class YiiChina
{
/**
* @var Curl
*/
protected $curl;
/**
* @var array
*/
protected $cookie = [];
/**
* @var string
*/
protected $csrf = '';
public function __construct()
{
$this->curl = new Curl();
$this->curl->setOpt(CURLOPT_SSL_VERIFYPEER,0);
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,0);
//设置 UA
$this->curl->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36');
}
/**
* getCsrfAndCookie
* @throws Exception
*/
protected function getCsrfAndCookie()
{
//请求登录页面
$this->curl->get(LoginUrl);
if ($this->curl->error) {
throw new \Exception('请求登录页面->Error: ' . $this->curl->errorCode . ': ' . $this->curl->errorMessage);
}
//登录成功获取cookie
$this->cookie = $this->curl->getResponseCookies();
//解析登录页面
$doc = phpQuery::newDocumentHTML($this->curl->response);
//获取防止 跨站请求伪造 加密字符串
$this->csrf = $doc->find('input[name=_csrf]')->val();
}
/**
* login
* @throws Exception
*/
protected function login()
{
//带上Referrer
$this->curl->setReferrer(SiteUrl);
//带上 请求 头
$this->curl->setHeader('X-Requested-With', 'XMLHttpRequest');
//带上 sessionId 的 cookie
$this->curl->setCookies($this->cookie);
$userInfo = [
'_csrf' => $this->csrf,
'LoginForm[username]' => UserName,
'LoginForm[password]' => PassWord,
'LoginForm[rememberMe]' => '1',
];
$this->curl->post(LoginUrl,$userInfo,1);
if ($this->curl->error) {
throw new \Exception('开始登录->Error: ' . $this->curl->errorCode . ': ' . $this->curl->errorMessage);
}
//获取 登录的时产生的 cookie
//$this->cookie = array_merge($this->cookie,$this->curl->getResponseCookies());
$this->cookie += $this->curl->getResponseCookies();
}
/**
* sign
* @return bool
*/
public function sign()
{
try {
$this->getCsrfAndCookie();
$this->login();
//带上Referrer
$this->curl->setReferrer(SiteUrl);
$this->curl->setHeader('X-Requested-With', 'XMLHttpRequest');
//带上 sessionId 的 cookie
$this->curl->setCookies($this->cookie);
//签到
$this->curl->post(SignInUrl,['_csrf'=>$this->csrf]);
if ($this->curl->error) {
throw new \Exception('开始签到->Error: ' . $this->curl->errorCode . ': ' . $this->curl->errorMessage);
}
$signInResponse = $this->curl->response;
//{"status":1,"message":"已连续1天"}
$responseArray = json_decode(json_encode($signInResponse),1);
if(($responseArray['status']??0) == 1){
return true;
}else{
throw new \Exception($responseArray['message']?:'签到失败.');
}
}catch (\Exception $e){
@file_put_contents(LOGFILE,date('Y-m-d H:i:s').'---'.$e->getMessage().PHP_EOL,FILE_APPEND);
return false;
}
}
}