>백엔드 개발 >PHP 튜토리얼 >使用 模拟登陆 抓取学校系统的晨跑数据,失败了,请教大神

使用 模拟登陆 抓取学校系统的晨跑数据,失败了,请教大神

WBOY
WBOY원래의
2016-06-23 13:57:081241검색

最近在学习php的模拟登陆,就想拿学校的体育教学系统实践一下,但失败了。请教大神,我的代码到底错在了哪?

【一些信息】
登录系统的地址:http://210.35.75.247:8080/student/studentFrame.jsp
(用户名:1101001,密码:1101001,角色:学生)
查晨跑的地址:http://210.35.75.247:8080/student/queryCheckInfo.jsp

【代码如下】

<?php  $cookie_file = tempnam('./temp','cookie');$login_url = 'http://210.35.75.247:8080/student/studentFrame.jsp';$post_fields  = 'userName=1101001&passwd=1101001';$ch = curl_init($login_url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);curl_exec($ch);curl_close($ch);$url = 'http://210.35.75.247:8080/student/queryCheckInfo.jsp';$ch = curl_init($url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);curl_exec($ch);curl_close($ch);?>


【第一次发帖,拜谢!】


回复讨论(解决方案)

你的代码没有大问题,curl_exec()返回的是结果,你没有接收没有输出,所以空白了
刚才试了一下,登陆成功了

<?php$url = 'http://210.35.75.247:8080/student/studentFrame.jsp';$name = '1101001';$pw = '1101001';$params = array(    'userName' => $name,    'passwd' => $pw,);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));$con = curl_exec($ch);curl_close($ch);echo preg_replace('/<frame src="\//Us', '<frame src="http://210.35.75.247:8080/', $con);

回楼上wander_wind:

谢谢!不过其实我想抓的是晨跑数据,所以就想通过模拟登陆把登录的cookie保存下来,再去这个地址(http://210.35.75.247:8080/student/queryCheckInfo.jsp)抓取晨跑次数。不知道为什么显示登录错误,能请大神再帮我看看吗?麻烦了!谢谢!


你的代码没有大问题,curl_exec()返回的是结果,你没有接收没有输出,所以空白了
刚才试了一下,登陆成功了

<?php$url = 'http://210.35.75.247:8080/student/studentFrame.jsp';$name = '1101001';$pw = '1101001';$params = array(    'userName' => $name,    'passwd' => $pw,);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));$con = curl_exec($ch);curl_close($ch);echo preg_replace('/<frame src="\//Us', '<frame src="http://210.35.75.247:8080/', $con);

别客气我也只是菜鸟,一起研究学习下
首先声明几个名词...
框架页 student/studentFrame.jsp 表单提交的action地址
学生信息页 student/studentInfo.jsp 是框架页的iframe自动加载的src
晨跑成绩 student/queryCheckInfo.jsp
用firebug跟踪了一下登陆流程,发现框架页的iframe加载的学生信息后面带了参数?username=...&passwd=....,猜测真正的登陆流程应该在学生信息页。我把登陆失败的页面不echo保存在文件中看了下,框架页的html和登陆成功几乎相同,只是多了一段跳转js。利用学生信息页的JSESSIONID登陆成功了,和框架页的对比下,不一样。蛋疼的管理系统

<?php$url1 = 'http://210.35.75.247:8080/student/studentInfo.jsp';$url2 = 'http://210.35.75.247:8080/student/queryCheckInfo.jsp';$name = '1101001';$pw = '1101001';$params = array(    'userName' => $name,    'passwd' => $pw,);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url1 . '?' . http_build_query($params));curl_setopt($ch, CURLOPT_HEADER, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$con = curl_exec($ch);preg_match('/Set-Cookie: (.*);/Us', $con, $match);$cookie = $match[1];curl_setopt($ch, CURLOPT_URL, $url2);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_COOKIE, $cookie);$con = curl_exec($ch);curl_close($ch);echo $con;

啊,原来如此,是 http://210.35.75.247:8080/student/studentInfo.jsp 在作怪。已经成功!拜谢!!

别客气我也只是菜鸟,一起研究学习下
首先声明几个名词...
框架页 student/studentFrame.jsp 表单提交的action地址
学生信息页 student/studentInfo.jsp 是框架页的iframe自动加载的src
晨跑成绩 student/queryCheckInfo.jsp
用firebug跟踪了一下登陆流程,发现框架页的iframe加载的学生信息后面带了参数?username=...&passwd=....,猜测真正的登陆流程应该在学生信息页。我把登陆失败的页面不echo保存在文件中看了下,框架页的html和登陆成功几乎相同,只是多了一段跳转js。利用学生信息页的JSESSIONID登陆成功了,和框架页的对比下,不一样。蛋疼的管理系统

<?php$url1 = 'http://210.35.75.247:8080/student/studentInfo.jsp';$url2 = 'http://210.35.75.247:8080/student/queryCheckInfo.jsp';$name = '1101001';$pw = '1101001';$params = array(    'userName' => $name,    'passwd' => $pw,);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url1 . '?' . http_build_query($params));curl_setopt($ch, CURLOPT_HEADER, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$con = curl_exec($ch);preg_match('/Set-Cookie: (.*);/Us', $con, $match);$cookie = $match[1];curl_setopt($ch, CURLOPT_URL, $url2);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_COOKIE, $cookie);$con = curl_exec($ch);curl_close($ch);echo $con;

貌似有分可以给
另外这里好像要结贴

哈哈,不好意思啊,第一次发帖不了解情况,已给分~ 谢谢!

貌似有分可以给
另外这里好像要结贴

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.