forked from xuyangmiaomiao/CET-Score
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CET-Score.php
111 lines (92 loc) · 3.49 KB
/
CET-Score.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
<?php
/**
* 这是一个查询 大学英语四六级成绩的类
* 抓取自 http://www.chsi.com.cn/cet/
* @read http://www.xuyangjie.cn/the-study/CET-Score.html
* @GitHub https://github.com/xuyangmiaomiao/CET-Score
* @webSite http://www.xuyangjie.cn/
* @email [email protected]
* @time 2014年9月3日 14:49:55
* @author 许杨淼淼
*/
error_reporting(0);
class getCetScore{
/**
* $zkzh numeric 准考证号 (长度:15位)
* $xm string 姓名 (长度:至少两个汉字)
* return $url string 请求的网址
*/
static private function getUrl($zkzh, $xm){
(is_numeric($zkzh) && (strlen($zkzh) === 15) && is_string($xm) && (strlen($xm) > 8)) ? $url = 'http://www.chsi.com.cn/cet/query?zkzh='.$zkzh.'&xm='.urlencode($xm) : $url = NULL;
return $url;
}
/**
* $url string 请求网址
* return $webPage string 抓取到的未处理的网页源码
*/
static private function getWebPage($url){
if(!is_null($url)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.chsi.com.cn/cet/');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0');
$webPage = curl_exec($ch);
curl_close($ch);
return $webPage;
}
return NULL; // $url 是 NULL 则返回 NULL
}
/**
* $webPage string 网页源码
* return $arrayData string 个人和四六级成绩等信息的数组(该数组尚不完美)
*/
static private function getArrayData($webPage){
if(isset($webPage)){
preg_match_all('/<table(.|\s)*?<\/table>/', $webPage, $matches);
preg_match_all('/(>)(.|\s)*?(<)/', $matches[0][1], $matches);
$search = array('<','>',':',chr(13).chr(10));
$result = str_replace($search, '', $matches[0]);
$content = array();
foreach($result as $value){ //去除 数组value 前后的空格
$content[] = trim($value);
}
$content = array_filter($content); // 删除数组空元素
$arrayData = array();
foreach($content as $value){ // 数组key重新排序
$arrayData[] = $value;
}
isset($arrayData[11]) ? $arrayData = $arrayData : $arrayData = NULL; // $arrayData[11]是总分,总分不存在即准考证号或姓名错误
return $arrayData;
}
return NULL; // $webPage 不存在 则返回 NULL
}
/**
* $arrayData string 个人和四六级成绩等信息的数组
* return string JSON格式数据
*/
static private function getJsonData($arrayData){
if(isset($arrayData)){
return json_encode(array('msg'=>'success','code'=>'200','content'=>$arrayData));
}
return json_encode(array('msg'=>'error','code'=>'400','content'=>''));
}
/**
* 查询四六级成绩唯一入口函数
* $zkzh numeric 准考证号
* $xm string 姓名
* return $result string JSON格式查询结果
*/
static public function index($zkzh, $xm){
$url = self::getUrl($zkzh, $xm);
$webPage = self::getWebPage($url);
$arrayData = self::getArrayData($webPage);
$result = self::getJsonData($arrayData);
return $result;
}
}
isset($_GET['num']) ? $zkzh = $_GET['num'] : die(json_encode(array('msg'=>'error','code'=>'400','content'=>'请输入准考证号')));
isset($_GET['name']) ? $xm = $_GET['name'] : die(json_encode(array('msg'=>'error','code'=>'400','content'=>'请输入姓名')));
echo getCetScore::index($zkzh, $xm);
?>