用php实现正方教务系统的模拟登陆

第一次用php实现模拟登陆真的是弄了好久,还挺好玩的就是,都是自己一点一点探索的收获还是慢慢的。到现在还有一点没弄明白的就是为什么不用提交验证码信息也能登陆,感觉可能是bug吧!

想要实现模拟登陆,首先要打开浏览器登录你想要模拟登陆的网站然后按F12去找到模拟登陆要提交的数据,像正方还有个隐藏的__VIEWSTATE的变量需要用正则表达式匹配找到这个变量的值,登陆进去了你就会在页面发现你的个人信息并在代码中注意保存你的cookie以便于跳转到另一个想要查询的页面,我的代码就是查询成绩的方法,有兴趣的可以与我交流一起探讨!

<span style="font-size:14px;"><?php
	$url = "http://jw.jxust.edu.cn"; 
	$username = ''; //设置自己的账号
	$password = ''; // 设置自己的密码
	$ch1 = curl_init();
	curl_setopt($ch1, CURLOPT_URL, $url);
	curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
	$data = curl_exec($ch1);
	curl_close($ch1);
	$pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*)"/'; //正则表达式匹配__VIEWSTATE
	preg_match_all($pattern, $data, $matches);
	$result = $matches[1][0];
	//print_r($result);
	$post1 = array(                     //必要的提交信息
		'__VIEWSTATE' => $result,
		'txtUserName' => $username,
		'TextBox2' => $password,
		'RadioButtonList1' => iconv('utf-8', 'gb2312', '学生'),
		'Button1' => ''
	);
	$filecookie = dirname(__FILE__)."/cookie";  
	$ch2 = curl_init();
	curl_setopt($ch2, CURLOPT_URL, $url);
	curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch2, CURLOPT_POST, 1);
	curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($post1));
	curl_setopt($ch2, CURLOPT_COOKIEJAR, $filecookie);    //保存登陆的cookie信息到filecookie文件中
	curl_exec($ch2);
	curl_close($ch2);

	//模拟登录成绩页面获取__VIEWSTATE
	$ch3 = curl_init();
	curl_setopt($ch3, CURLOPT_URL, "http://jw.jxust.edu.cn/xscj_gc.aspx?xh=$username&gnmkdm=N121605");
	curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch3, CURLOPT_COOKIEFILE, $filecookie);
	curl_setopt($ch3, CURLOPT_REFERER, "http://jw.jxust.edu.cn");
	$data = curl_exec($ch3);
	curl_close($ch3);
	$pattern = '/<input type="hidden" name="__VIEWSTATE" value="(.*)"/';   
	/*  又要获取__VIEWSTATE,  注意每个模块的的__VIEWSTATE都不一样  */
	preg_match_all($pattern, $data, $matches);
	$cj__VIEWSTATE = $matches[1][0];


	//模拟登陆获取成绩信息
	$ch4 = curl_init();
	curl_setopt($ch4, CURLOPT_URL, "http://jw.jxust.edu.cn/xscj_gc.aspx?xh=$username&gnmkdm=N121605");
	$post2 = array(     //提交要查找的信息的数据
		'__VIEWSTATE' => $cj__VIEWSTATE,
		'ddlXN' => '',
		'ddlXQ' => '', 
		'Button1' => iconv('utf-8', 'gb2312', '按学期查询') 
	);
	curl_setopt($ch4, CURLOPT_RETURNTRANSFER, 0);
	curl_setopt($ch4, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch4, CURLOPT_COOKIEFILE, $filecookie);
	curl_setopt($ch4, CURLOPT_POST, 1);
	curl_setopt($ch4, CURLOPT_POSTFIELDS, $post2);
	curl_setopt($ch4, CURLOPT_REFERER, "http://jw.jxust.edu.cn");
	$data = curl_exec($ch4);
	//preg_match_all($pattern, $data, $matches);
	curl_close($ch4);
?></span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值