blob.png

blob.png

首先要阅读 验证码识别

把属于你们的识别验证码的PHP拿出来,

然后我们引用 PHP获取成绩 即 如下

----PHP获取成绩(旧)-开始---

<?php
//即没有验证码的
$id='';
$psw='';
if($_POST["id"]){
global $id;
$id=$_POST["id"];
}
if($_POST["psw"]){
global $psw; 
$psw=$_POST["psw"];
}
$view2='';
$urlcookie='';
$name='';
function curl_request($url,$post='',$cookie='', $returnCookie=0){
global $id;
$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://教务系统地址/xs_main.aspx?xh='.$id);
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);
if (curl_errno($curl)) {
return curl_error($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);
global $urlcookie;
$urlcookie=substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
}else{
return $data;
}
}
function getView(){
$res;
$url='http://教务系统地址/default2.aspx';
$result=curl_request($url);
$pattern='/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is'; 
preg_match_all($pattern, $result, $matches);
$res=$matches[1][0];
return $res;
}
function login(){
global $id;
global $psw;
$url = "http://教务系统地址/default2.aspx";
$post['__VIEWSTATE'] = getView();
$post['TextBox1'] =$id;
 $post['TextBox2'] =$psw;
$post['RadioButtonList1'] = '学生';
$post['Button1'] = '登录';
$result = curl_request($url,$post,'', 1);
return $result;
}
function main(){
login();
global $id;
global $urlcookie;
global $view2;
$url="http://教务系统地址/xscj_gc.aspx?xh={$id}&gnmkdm=N121605";
 $result = curl_request($url,'',$urlcookie);
$pattn='/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is'; 
preg_match_all($pattn, $result, $ma);
$view2=$ma[1][0];
global $name; 
$pa='#<span[^>]*?xhxm[^>]*?>([^<]*?)</span>#is';
preg_match_all($pa, $result, $matche);
$res=$matche[1][0];
$name = substr ($res, 0,-4);
}
function get_td_array($table) {
$table = preg_replace("'<table[^>]*?>'si","",$table);
$table = preg_replace("'<tr[^>]*?>'si","",$table);
$table = preg_replace("'<td[^>]*?>'si","",$table);
$table = str_replace("</tr>","{tr}",$table);
$table = str_replace("</td>","{td}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'([rn])[s]+'","",$table);
$table = preg_replace('/&nbsp;/',"",$table);
$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode('{tr}', $table);
array_pop($table);
foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
function cj(){
main();
global $id;
global $urlcookie;
global $name;
 global $view2;
$url="http://教务系统地址/xscj_gc.aspx?xh={$id}&gnmkdm=N121605";
$post['ddlXN'] = '2014-2015';
$post['ddlXQ'] = '2';
$post['__VIEWSTATE'] = $view2;
$post['Button1'] ='按学期查询';
$result =curl_request($url,$post,$urlcookie);
$result=get_td_array($result);
foreach($result as $v){
if($v[8]){
$grade .="<tr><td width=\"200px\" align=\"center\" >{$v[3]}</td><td align=\"center\" >{$v[8]}</td></tr>";
}
}
print_r("<table>{$grade}</table>");
}
echo cj();
?>

----PHP获取成绩(旧)-结束---

添加识别验证码如下

<?php
//含验证码识别
$id='';
$psw='';
if($_POST["id"]){
global $id;
$id=$_POST["id"];
}
if($_POST["psw"]){
global $psw; 
$psw=$_POST["psw"];
}
$view2='';
$urlcookie='';
$name='';
function curl_request($url,$post='',$cookie='', $returnCookie=0){
global $id;
$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://教务系统地址/xs_main.aspx?xh='.$id);
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);
if (curl_errno($curl)) {
return curl_error($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);
global $urlcookie;
$urlcookie=substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
}else{
return $data;
}
}
function getView(){
$res;
$url='http://教务系统地址/default2.aspx';
/*-----此处有改动------*/
$result=curl_request($url,'','',1); //让服务器返回cookie信息   
/*---------------------*/
$pattern='/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is'; 
preg_match_all($pattern, $result, $matches);
$res=$matches[1][0];
return $res;
}
/*-----验证码识别方法如下------*/
function yzm($c){
$url = "http://www.kejibu.org/cx/cts.php?c={$c}";  
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
/*-----------------------------*/
function login(){
global $id;
global $psw;
$url = "http://教务系统地址/default2.aspx";
$post['__VIEWSTATE'] = getView();
$post['TextBox1'] =$id;
$post['TextBox2'] =$psw;
/*-----验证码如下------*/
global $urlcookie;
$post['TextBox3'] =yzm($urlcookie); //带入cookie去执行yzm方法
/*---------------------*/
$post['RadioButtonList1'] = '学生';
$post['Button1'] = '登录';
/*-----此处有改动------*/
$result = curl_request($url,$post,$urlcookie);
/*---------------------*/
return $result;
}
function main(){
login();
global $id;
global $urlcookie;
global $view2;
$url="http://教务系统地址/xscj_gc.aspx?xh={$id}&gnmkdm=N121605";
 $result = curl_request($url,'',$urlcookie);
$pattn='/<input type="hidden" name="__VIEWSTATE" value="(.*?)" \/>/is'; 
preg_match_all($pattn, $result, $ma);
$view2=$ma[1][0];
global $name; 
$pa='#<span[^>]*?xhxm[^>]*?>([^<]*?)</span>#is';
preg_match_all($pa, $result, $matche);
$res=$matche[1][0];
$name = substr ($res, 0,-4);
}
function get_td_array($table) {
$table = preg_replace("'<table[^>]*?>'si","",$table);
$table = preg_replace("'<tr[^>]*?>'si","",$table);
$table = preg_replace("'<td[^>]*?>'si","",$table);
$table = str_replace("</tr>","{tr}",$table);
$table = str_replace("</td>","{td}",$table);
//去掉 HTML 标记
$table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);
//去掉空白字符
$table = preg_replace("'([rn])[s]+'","",$table);
$table = preg_replace('/&nbsp;/',"",$table);
$table = str_replace(" ","",$table);
$table = str_replace(" ","",$table);
$table = explode('{tr}', $table);
array_pop($table);
foreach ($table as $key=>$tr) {
$td = explode('{td}', $tr);
array_pop($td);
$td_array[] = $td;
}
return $td_array;
}
function cj(){
main();
global $id;
global $urlcookie;
global $name;
 global $view2;
$url="http://教务系统地址/xscj_gc.aspx?xh={$id}&gnmkdm=N121605";
$post['ddlXN'] = '2014-2015';
$post['ddlXQ'] = '2';
$post['__VIEWSTATE'] = $view2;
$post['Button1'] ='按学期查询';
$result =curl_request($url,$post,$urlcookie);
$result=get_td_array($result);
foreach($result as $v){
if($v[8]){
$grade .="<tr><td width=\"200px\" align=\"center\" >{$v[3]}</td><td align=\"center\" >{$v[8]}</td></tr>";
}
}
print_r("<table>{$grade}</table>");
}
echo cj();
?>