-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfss.php
360 lines (308 loc) · 8.81 KB
/
dfss.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
*执行类,程序入口
*整个程序以一定时间间隔持续刷新预约单,一直到设定的时间段检测到有可预约状态,提交预约单
*程序中调整hasFreeTime方法,可以设定一天几个预约时间段的优先级
*@author bitpanda
*/
include("configure.php");
include("utils.php");
$isSuccess = false;
$configure = new configure();
$LOG = new Log();
$dfss = new dfss($LOG,$configure);
while(!$isSuccess)
{
if(!$dfss->isLogin)
$dfss->login();
if(!$dfss->isLogin)
{
$LOG->log("Relogin failed,sleep 1s");
sleep(1);
continue;
}
$dfss->accessToData();
$LOG->log("打开约车页面");
while(!$isSuccess && $dfss->isLogin)
{
$LOG->log("刷新约车页面");
if($dfss->refreshData())
{
$LOG->log("判断有无空闲时间段");
if($dfss->hasFreeTime())
{
$LOG->log("提交表单");
if($dfss->postData())
{
$LOG->log("约车成功,退出");
$isSuccess = true;
break;
}
else
{
$LOG->log("约车失败,POSTERROR");
}
}
$LOG->log("无空闲时间段,3s后刷新");
// sleep(3);
}
else
{
//$dfss->isLogin = false;
$LOG->log("Refresh data error,sleep 5s");
sleep(3);
continue;
}
}
sleep(3);
}
class dfss
{
var $isLogin = false;
public function __construct($log = "", $configure="")
{
$this->isLogin = false;
$this->configure = $configure;
$this->LOG = $log;
$this->init();
}
public function init()
{
$this->ch = curl_init();
// 获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->configure->header);
curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0');
curl_setopt($this->ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 5);
//get cookie info
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->configure->cookieFile);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->configure->cookieFile);
}
public function login()
{
$this->LOG->log("login....");
$post = $this->getLoginParameters();
if($post != null)
{
curl_setopt($this->ch, CURLOPT_URL, $this->configure->loginUrl);
//发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
$loginResult = curl_exec($this->ch);
if ($loginResult == NULL) {
$this->LOG->log("连接网络失败");
}
else
{
$this->LOG->saveFile($loginResult, $this->configure->login_result_html);
$analyze = new analyze($loginResult);
$this->isLogin = $analyze->isLoginSuccess();
}
}
}
public function getLoginParameters()
{
$u = $this->configure->username;
$p = $this->configure->password;
//连接登陆页面获取表单元素,构造post参数
curl_setopt($this->ch, CURLOPT_URL, $this->configure->loginUrl);
curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
$loginHtml = curl_exec($this->ch);
//获取验证码图片
curl_setopt($this->ch, CURLOPT_URL, $this->configure->captchaUrl);
curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
$captchaImg = curl_exec($this->ch);
if($loginHtml == null || $captchaImg == null)
{
$this->LOG->log("连接网络失败");
return null;
}
else
{
$this->LOG->log("连接网络成功");
$this->LOG->saveFile($loginHtml, $this->configure->login_html);
$this->LOG->saveFile($captchaImg, $this->configure->captcha_png);
$analyze = new analyze($loginHtml);
$viewState = urlencode($analyze->getViewState());
$eventValidation = urlencode($analyze->getEventValidation());
$c = $this->decaptcha();
return "__VIEWSTATE={$viewState}".
"&txtname={$u}".
"&txtpwd={$p}".
"&yanzheng={$c}".
"&button.x=0&button.y=0".
"&__EVENTVALIDATION={$eventValidation}";
}
}
public function decaptcha()
{
exec("{$this->configure->tesseract_path} {$this->configure->captcha_png} {$this->configure->decode_path}");
$handle = fopen($this->configure->decode_path.'.txt', 'r');
if($handle != null)
{
$line = fgets($handle);
$this->LOG->log("自动识别验证码:".substr($line, 0, 5));
return substr($line, 0, 5);
}
exec($this->configure->captcha_png);
$handle = fopen('php://stdin', 'r');
echo "请输入验证码:";
$line = fgets($handle);
$this->LOG->log("手动输入验证码:".substr($line, 0, 5));
return substr($line, 0, 5);
}
public function accessToData()
{
curl_setopt($this->ch, CURLOPT_URL, $this->configure->yuecheUrl);
curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
$reserveHtml = curl_exec($this->ch);
if($reserveHtml == null)
{
$this->LOG->log("约车页面打开失败");
$this->isLogin = false;
}
else
{
$this->LOG->saveFile($reserveHtml, $this->configure->yueche_html);
$analyze = new analyze($reserveHtml);
if($analyze->isOpenReserveSuccess())
{
$this->reserve_V = urlencode($analyze->getViewState());
$this->reserve_E = urlencode($analyze->getEventValidation());
}
else
{
$this->LOG->log("约车页面出错:".$analyze->getErrorMsg());
$this->isLogin = false;
}
}
}
public function refreshData()
{
$post = "__EVENTTARGET="
."&__EVENTARGUMENT="
."&__LASTFOCUS="
."&__VIEWSTATE={$this->reserve_V}"
."&RadioButtonList1={$this->configure->refreshField}"
."&btnRefresh={$this->configure->refreshField}"
."&__EVENTVALIDATION={$this->reserve_E}";
curl_setopt($this->ch, CURLOPT_URL, $this->configure->yuecheUrl);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
$refreshHtml = curl_exec($this->ch);
if($refreshHtml == null)
{
$this->LOG->log("刷新约车表单时失败");
$this->isLogin = false;
return false;
}
else
{
$this->LOG->saveFile($refreshHtml, $this->configure->refresh_html);
$analyze = new analyze($refreshHtml);
if($analyze->isOpenReserveSuccess())
{
$this->reserve_V = urlencode($analyze->getViewState());
$this->reserve_E = urlencode($analyze->getEventValidation());
$this->reserveArray = $analyze->getReserveArray();
return true;
}
else
{
$this->LOG->log("刷新约车页面出错:".$analyze->getErrorMsg());
$this->isLogin = false;
return false;
}
}
}
public function hasFreeTime()
{
$dateList = $this->configure->dateList;
$dateInfoList = $this->reserveArray;
$this->LOG->log("进入判断:");
foreach($dateInfoList as $dateInfo)
{
print_r($dateInfo);
foreach($dateList as $date)
{
if(strcmp("{$dateInfo->date}", "{$date}")==0)
{
$this->LOG->log($dateInfo->date." ".$date);
if($dateInfo->isLeft())
{
$this->LOG->log($dateInfo->date."有剩余");
if($dateInfo->value1 > 0)
{
$this->selectValue = urlencode($dateInfo->value1);
$this->selectName = urlencode($dateInfo->name1);
}
elseif($dateInfo->value2 > 0)
{
$this->selectValue = urlencode($dateInfo->value2);
$this->selectName = urlencode($dateInfo->name2);
}
elseif($dateInfo->value3 > 0)
{
$this->selectValue = urlencode($dateInfo->value3);
$this->selectName = urlencode($dateInfo->name3);
}
elseif($dateInfo->value4 > 0)
{
$this->selectValue = urlencode($dateInfo->value4);
$this->selectName = urlencode($dateInfo->name4);
}
elseif($dateInfo->value5 > 0)
{
$this->selectValue = urlencode($dateInfo->value5);
$this->selectName = urlencode($dateInfo->name5);
return false;
}
return true;
}
}
}
}
return false;
}
public function postData()
{
$post = "__EVENTTARGET="
."&__EVENTARGUMENT="
."&__LASTFOCUS="
."&__VIEWSTATE={$this->reserve_V}"
."&RadioButtonList1={$this->configure->postField}"
."&{$this->selectName}={$this->selectValue}"
."&__EVENTVALIDATION={$this->reserve_E}";
curl_setopt($this->ch, CURLOPT_URL, $this->configure->yuecheUrl);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
$postResultHtml = curl_exec($this->ch);
if($postResultHtml == null)
{
$this->LOG->log("提交表单时失败,页面为空");
return false;
}
else
{
$this->LOG->saveFile($postResultHtml, $this->configure->yueche_result_html);
$analyze = new analyze($postResultHtml);
if($analyze->isReserveSuccess())
{
// $this->reserve_V = urlencode($analyze->getViewState());
// $this->reserve_E = urlencode($analyze->getEventValidation());
// $this->reserveArray = $analyze->getReserveArray();
return true;
}
else
{
$this->LOG->log("提交表单出错:".$analyze->getErrorMsg());
return false;
}
}
}
}
?>