Curl获取教务系统数据

5.30更新

查询课程表需添加字段 $loginParams['TextBox1'] = $password;
查询成绩..还没搞定..不想搞了..反正这学期也差不多了
妈的 一学期教务系统改好几次,有病啊


前言

  1. 获取验证码时需保存cookie,提交验证码时要一同提交
  2. 提交验证码时还需要获取网页内的_viewstate的值(据观察这个值是每天变化一次的)
  3. post所需要提交的字段

    • 课程表

      • $loginParams[‘__VIEWSTATE’] = $getview[0];
      • $loginParams[‘txtUserName’] = $user;
      • $loginParams[‘TextBox2’] = $password;
      • $loginParams[‘txtSecretCode’] = $code;
      • $loginParams[‘RadioButtonList1’] = ‘学生’;
      • $loginParams[‘Button1’] = ‘’;
      • $loginParams[‘lbLanguage’] = ‘’;
      • $loginParams[‘hidPdrs’] = ‘’;
      • $loginParams[‘hidsc’] = ‘’;
    • 成绩

      • $logParams[‘__VIEWSTATE’] = $res[0];
      • $logParams[‘ddlXN’] =$xuenian;
      • $logParams[‘ddlXQ’] =$xueqi;
      • $logParams[‘Button1’] = “按学期查询”;
      • $logParams[‘txtQSCJ’]=”0”;
      • $logParams[‘txtZZCJ’]=”100”;

调用函数部分

变量初始化

1
2
3
4
5
6
$act='';
$xuenian="";
$xueqi="";
$user = '';//用户名
$password = '';//密码
define('SCRIPT_ROOT',dirname(__FILE__).'/');

curl封装函数

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
function curl_request($url,$post='',$cookie='', $returnCookie=0){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
if($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
if($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);

curl_close($curl);
if($returnCookie){
list($header, $body) = explode("\r\n\r\n", $data, 2);
preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
$info['cookie'] = substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
}else{
return $data;
}
}

获取_viewstate函数

1
2
3
4
5
6
7
8
9
10
function getView(){

$url = 'http://jwxt.jit.edu.cn/default2.aspx';
$result =curl_request($url);
$pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is';
preg_match_all($pattern, $result, $matches);
$res[0] = $matches[1][0];
// print_r($res);
return $res;
}

获取验证码

1
showAuthcode('http://jwxt.jit.edu.cn/CheckCode.aspx');

获取验证码图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

function showAuthcode( $authcode_url )
{
$cookieFile = SCRIPT_ROOT.'cookie.tmp';
$ch = curl_init($authcode_url);
curl_setopt($ch,CURLOPT_COOKIEJAR, $cookieFile); // 把返回来的cookie信息保存在文件中
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, '');
curl_setopt($ch, CURLOPT_USERAGENT, 'Baiduspider');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content =curl_exec($ch);
header('Content-type: image/jpeg');
print_r( $content);
//var_dump($cookieFile);
curl_close($ch);
}

读取课程表

调用函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 $getview=getView();  //执行获取viewstate
// print_r($getview[0]);
$user=trim($_REQUEST['username']);
$password=trim($_REQUEST['password']);
$code = trim($_REQUEST['code']);
// $loginParams为curl模拟登录时post的参数
$loginParams['__VIEWSTATE'] = $getview[0];
$loginParams['txtUserName'] = $user;
$loginParams['TextBox2'] = $password;
$loginParams['txtSecretCode'] = $code;
$loginParams['RadioButtonList1'] = '学生';
$loginParams['Button1'] = '';
$loginParams['lbLanguage'] = '';
$loginParams['hidPdrs'] = '';
$loginParams['hidsc'] = '';
// $cookieFile 为加载验证码时保存的cookie文件名
$cookieFile = SCRIPT_ROOT.'cookie.tmp';
// $targetUrl curl 提交的目标地址
$targetUrl = 'http://jwxt.jit.edu.cn/default2.aspx';
// 参数重置
$content = curlLogin($targetUrl, $cookieFile, $loginParams);
echo $content;
break;

获取课程表函数

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
function curlLogin($url, $cookieFile, $loginParams)
{
global $user;
global $password;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);//设定返回的数据是否自动显示
curl_setopt($ch, CURLOPT_HEADER, 0);//设定是否显示头信 息
curl_setopt($ch, CURLOPT_NOBODY, false);//设定是否输出页面 内容
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $loginParams); //提交查询信息
curl_exec($ch);//返回结果
curl_close($ch); //关闭
$curl2=curl_init();
curl_setopt ($curl2,CURLOPT_REFERER,'http://jwxt.jit.edu.cn/xs_main.aspx?xh='.$user);
curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl2, CURLOPT_HEADER, false);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl2, CURLOPT_TIMEOUT, 20);
curl_setopt($curl2, CURLOPT_AUTOREFERER, true);
curl_setopt($curl2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl2, CURLOPT_URL, 'http://jwxt.jit.edu.cn/xskbcx.aspx?xh='.$user);//登陆后要从哪个页面获取信息
$en_contents=mb_convert_encoding( curl_exec($curl2),'utf-8', array('Unicode','ASCII','GB2312','GBK','UTF-8'));
//print_r($en_contents);


preg_match_all('/<span id="Label[^>]*>(.*)<\/span>/isU',$en_contents,$out);


$student = explode(':', $out[1][6]);
$studentInfo[0] = $student[1];
$student = explode(':', $out[1][5]);
$studentInfo[1] = $student[1];
$student = explode(':', $out[1][2]);
$studentInfo[2] = substr(trim($student[1]), 0,4);
// var_dump($studentInfo);
preg_match_all('/<table id="Table1"[\w\W]*?>([\w\W]*?)<\/table>/',$en_contents,$out);

$table = $out[0][0];

//检查
if($table==''){
echo "<script>alert('是不是输错了啦QAQ');</script>";

return;
}
preg_match_all('/<td [\w\W]*?>([\w\W]*?)<\/td>/',$table,$out);

$td = $out[1];
// print_r($table);
$length = count($td);
//获得课程列表
for ($i=0; $i < $length; $i++) {
$td[$i] = str_replace("<br>", "", $td[$i]);
$reg = "/{(.*)}/";
if (!preg_match_all($reg, $td[$i], $matches)) {
unset($td[$i]);
}
}
$data = array_values($td); //将课程列表数组重新索引
$tdLength = count($td);
//将课表转换成数组形式
for($i=0;$i<count($data);$i++) {
$data[$i] = str_replace('(' , '(' , $data[$i]);
$data[$i] = str_replace(')' , ')' , $data[$i]);
preg_match_all('/(?!\d+$)[0-9A-z]{4}/', $data[$i], $classroom);

if (count($classroom[0]) == 2) {
// var_dump($data[$i]);
$match = preg_split("/{$classroom[0][0]}/", $data[$i],2);
$match1=preg_split("/\)/", $match[1],2);
$data[$i] = str_replace($match1[1] , '' , $data[$i]);
array_push($data,$match1[1]);

}

}
// var_dump($data);
//var_dump($data);


$teacher='';
for($i=0;$i<count($data);$i++){
$str =$data[$i];
preg_match_all('/(.*?)周(.*?)第/',$str,$subject);
preg_match_all('/周(.*?)第/',$str,$day);
preg_match_all('/第(.*?),(.*?)节/',$str,$time);


preg_match_all('/\{第(.*?)-(.*?)周\}/',$str,$week);

preg_match_all('/(?!\d+$)[0-9A-z]{4}/',$str,$classroom);

$kcb[$i][0]=$subject[1][0];
$kcb[$i][1]=$day[1][0];
$kcb[$i][2]=$time[1][0];
$kcb[$i][3]=$time[2][0];
$kcb[$i][4]=$week[1][0];
$kcb[$i][5]=$week[2][0];
$kcb[$i][6]=$teacher;
$kcb[$i][7]=$classroom[0][0];
}

$json_string = json_encode($kcb);
// 写入文件
file_put_contents('kcb.json', $json_string);
// echo "<pre>";
//var_dump($kcb);
curl_close($curl2);
$url = "showkcb.php";
header("Location: $url");
}

读取成绩

调用函数

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
$getview=getView();
// print_r($getview[0]);
$user=trim($_REQUEST['username']);
$password=trim($_REQUEST['password']);
$code = trim($_REQUEST['code']);
$xuenian=trim($_REQUEST['xuenian']);
$xueqi=trim($_REQUEST['xueqi']);
// $loginParams为curl模拟登录时post的参数
$loginParams['__VIEWSTATE'] = $getview[0];
$loginParams['txtUserName'] = $user;
$loginParams['TextBox2'] = $password;
$loginParams['txtSecretCode'] = $code;
$loginParams['RadioButtonList1'] = '学生';
$loginParams['Button1'] = '';
$loginParams['lbLanguage'] = '';
$loginParams['hidPdrs'] = '';
$loginParams['hidsc'] = '';

// $cookieFile 为加载验证码时保存的cookie文件名
$cookieFile = SCRIPT_ROOT.'cookie.tmp';
// $targetUrl curl 提交的目标地址
$targetUrl = 'http://jwxt.jit.edu.cn/default2.aspx';
// 参数重置
$content = curlLogin1($targetUrl, $cookieFile, $loginParams);
echo '学年'.$xuenian."学期".$xueqi;
break;

查询成绩函数

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
function curlLogin1($url, $cookieFile, $loginParams)
{
global $xuenian;
global $xueqi;
global $user;
global $password;
// $user = $user;//用户名
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);//设定返回的数据是否自动显示
curl_setopt($ch, CURLOPT_HEADER, 0);//设定是否显示头信 息
curl_setopt($ch, CURLOPT_NOBODY, false);//设定是否输出页面 内容
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $loginParams); //提交查询信息
curl_exec($ch);//返回结果
curl_close($ch); //关闭
$curl2=curl_init();
curl_setopt ($curl2,CURLOPT_REFERER,'http://jwxt.jit.edu.cn/xs_main.aspx?xh='.$user);
curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl2, CURLOPT_HEADER, false);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl2, CURLOPT_TIMEOUT, 20);
curl_setopt($curl2, CURLOPT_AUTOREFERER, true);
curl_setopt($curl2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl2, CURLOPT_URL, 'http://jwxt.jit.edu.cn/xscjcx_dq.aspx?xh='.$user);//登陆后要从哪个页面获取信息
$en_contents=mb_convert_encoding( curl_exec($curl2),'utf-8', array('Unicode','ASCII','GB2312','GBK','UTF-8'));
curl_close($curl2);
//print_r($en_contents);
$pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is';
preg_match_all($pattern, $en_contents, $matches);
$res[0] = $matches[1][0];
// print_r($res);
$logParams['__EVENTTARGET'] = "";
$logParams['__EVENTARGUMENT'] = "";
$logParams['__VIEWSTATE'] = $res[0];
$logParams['ddlxn'] =$xuenian;
$logParams['ddlxq'] =$xueqi;
$logParams['btnCx'] = " 查 询 ";
$curl3=curl_init();
curl_setopt ($curl3,CURLOPT_REFERER,'http://jwxt.jit.edu.cn/xscjcx_dq.aspx?xh='.$user);
curl_setopt($curl3, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl3, CURLOPT_HEADER, false);
curl_setopt($curl3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl3, CURLOPT_TIMEOUT, 20);
curl_setopt($curl3, CURLOPT_AUTOREFERER, true);
curl_setopt($curl3, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl3,CURLOPT_POSTFIELDS, $logParams); //提交查询信息
curl_setopt($curl3, CURLOPT_URL, 'http://jwxt.jit.edu.cn/xscjcx_dq.aspx?xh='.$user);//登陆后要从哪个页面获取信息
$en_contents=mb_convert_encoding( curl_exec($curl3),'utf-8', array('Unicode','ASCII','GB2312','GBK','UTF-8'));

curl_close($curl3);
preg_match_all('/<table class="datelist"[\w\W]*?>([\w\W]*?)<\/table>/',$en_contents,$xscj);
$xscj1=$xscj[0][0];
//检查
if($xscj1==''){
echo "<script>alert('是不是输错了啦QAQ');</script>";

return;
}
preg_match_all('/<tr[\w\W]*?>([\w\W]*?)<\/tr>/',$xscj1,$xscj2);

$json_string = json_encode($xscj2[0]);

// 写入文件
file_put_contents('cj.json', $json_string);
//print_r($xscj1);
$url = "showcj.php";
header( "Location: $url" );


}

HTML表单部分

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
<form role="form">

<input type="hidden" name="act" value="" id="hidden">
<span>输入学号:</span><input class ="form-control" type="text" name="username" /><br>
<span>输入密码:</span><input class ="form-control" type="password" name="password" /><br>
<iframe class="pull-right" src="?act=authcode" style='width: 100px; height:30px; overflow: hidden; display:block;' frameborder=0 ></iframe><br>
<span>输入验证码:</span><input class ="form-control" type="text" name="code" /><br><br>

<div style="margin:0 auto;width:100px">
<input class ="form-control btn btn-primary" id="sub1" type="submit" name="submit" value="查询课表"><br><br>


<input class ="form-control btn btn-primary" type=button onclick="javascript:window.location.href='show1.php';" value="首页/刷新">
</div>


</form>

<script src="css/domain.js"></script>
<script>
var hidden=document.getElementById("hidden");
var sub1=document.getElementById("sub1");
var sub2=document.getElementById("sub2");


sub1.onclick=function () { hidden.value="login";};
sub2.onclick=function () { hidden.value="cxcj";};
// hidden.value="cxcj";
// alert(hidden.value);
</script>

效果

课程表
课程表