php模拟登陆青果教务系统,模拟登录 - php CURL模拟登陆正方教务系统

代码如下

$cookie_file = tempnam('./temp','cookie');

$login_url = 'http://211.64.47.129/default_ysdx.aspx';

$post_fields = '__VIEWSTATE=dDw1MjQ2ODMxNzY7Oz7xlHJHd0KfeVRA2p7BXNto118wbQ==&TextBox1=学号&TextBox2=密码';

$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://211.64.47.129/xs_main.aspx?xh=学号';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$contents = curl_exec($ch);

preg_match("/

(.*)/",$contents,$arr);

echo $arr[1];

curl_close($ch);

但是最后却回到了登录的界面,小白求大神解答

回复内容:

代码如下

$cookie_file = tempnam('./temp','cookie');

$login_url = 'http://211.64.47.129/default_ysdx.aspx';

$post_fields = '__VIEWSTATE=dDw1MjQ2ODMxNzY7Oz7xlHJHd0KfeVRA2p7BXNto118wbQ==&TextBox1=学号&TextBox2=密码';

$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://211.64.47.129/xs_main.aspx?xh=学号';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$contents = curl_exec($ch);

preg_match("/

(.*)/",$contents,$arr);

echo $arr[1];

curl_close($ch);

但是最后却回到了登录的界面,小白求大神解答

根据楼主的要求, 配合我自己写的 HttpClient 的类, 编写的代码如下, 但因为没有测试账号, 所以测试时使用的账号和密码为 test, 返回的结果是登陆失败, 楼主只需要修改代码里的账号和密码, 应该就可以了.

HTTP 请求过程中的 Cookie 由 HttpClient/CURL 自动处理.

楼主发的代码应该是没问题的, 感觉应该是没有提交 RadioButtonList1 和 Button1 这两个数据.

还有那个__VIEWSTATE 虽然看起来是固定的, 但为了保险期间, 还是应该从页面中获取到之后, 再提交登陆.

附代码:

ch = curl_init();

curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; QQDownload 685; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)');//UA

curl_setopt($this->ch, CURLOPT_TIMEOUT, 40);

curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);

curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);

curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_jar);

curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_jar);

}

function __destruct(){

curl_close($this->ch);

}

final public function setReferer($ref=''){

if($ref != ''){

curl_setopt($this->ch, CURLOPT_REFERER, $ref);

}

}

final public function Get($url, $header=false, $nobody=false){

curl_setopt($this->ch, CURLOPT_POST, false);

curl_setopt($this->ch, CURLOPT_URL, $url);

curl_setopt($this->ch, CURLOPT_HEADER, $header);

curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);

return curl_exec($this->ch);

}

final public function Post($url, $data=array(), $header=false, $nobody=false){

curl_setopt($this->ch, CURLOPT_URL, $url);

curl_setopt($this->ch, CURLOPT_HEADER, $header);

curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);

curl_setopt($this->ch, CURLOPT_POST, true);

curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));

return curl_exec($this->ch);

}

}

const Login_URL = 'http://211.64.47.129/default_ysdx.aspx';

$http = new HttpClient(tempnam('./temp','cookie'));

$html = $http->Get(Login_URL);//先请求登陆页面, 获取 __VIEWSTATE

preg_match('/name="__VIEWSTATE" value="(.+?)"/', $html, $vs);

if(count($vs) !== 2){

echo '获取viewstate失败';

exit();

}

//构造登陆时的数据

$data = array(

'__VIEWSTATE'=>$vs[1],//__VIEWSTATE

'TextBox1'=>'username',//修改此处的用户

'TextBox2'=>'password',//和密码

'RadioButtonList1'=>'学生',//以及身份类型

'Button1'=>' 登录 '

);

$html = $http->Post(Login_URL, $data);

preg_match('/language=\'javascript\'>alert\(\'(.+?)\'\);/', $html, $err);

//检测是否出错, 如果有出错, 则显示错误信息, 然后退出

if(count($err) === 2){

echo $err[1];

exit();

}

$sn = '123123';//学号

$html = $http->Get('http://211.64.47.129/xs_main.aspx?xh='. $sn);

preg_match('/

\s*(.*)/', $html, $result);

var_dump($result);

https://github.com/lndj/Lcrawl/tree/dev

一只优雅的正方教务系统爬虫。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值