首页 > 编程 > PHP > 正文

php 新浪通行证、新浪微博模拟统一登录 (后台网页抓取版) 2016

2020-03-22 18:47:04
字体:
来源:转载
供稿:网友
前几天做了一个Java的新浪通行证模拟登录测试。现在给大家一个php的新浪通行证、微博登录的示例:具体都有备注,大家阅读代码吧.

 'login.sina.com.cn','User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0','Accept' => '*/*','Accept-Language' => 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3','Accept-Encoding' => 'gzip, deflate','Referer' => 'http://login.sina.com.cn/','Connection' => 'keep-alive' );//base64加密后用户名private $su = '';//js加密后的密码private $sp = '';function __construct() {}//预登陆function pre_login() {// 用户名称base64加密-用于预登陆$this->su = base64_encode ( urlencode ( $this->username ) );// 预登陆url$pre_login_url = 'http://login.sina.com.cn/sso/prelogin.php?entry=account&callback=sinaSSOController.preloginCallBack&su=';$pre_login_url = $pre_login_url . $this->su . '&rsakt=mod&client=ssologin.js(v1.4.15)&_=' . time ();$return_val = $this->request_url ( $pre_login_url, null, $this->request_cookie, $this->request_headers);list ( $header, $body ) = explode ( "/r/n/r/n", $return_val, 2 );preg_match_all ( "/Set/-html' target='_blank'>Cookie:([^;]*);/", $header, $matches );$info ['cookie'] = $matches;$info ['header'] = $header;$info ['content'] = $body;$this->request_cookie .= $matches;$body = str_replace('sinaSSOController.preloginCallBack(', '', $body);$json = str_replace(')', '', $body);$this->json_obj = json_decode($json);//ajax后变量重置,所以存到cookieparam::set_cookie('sina_su', $this->su);param::set_cookie('sina_cookie', $this->request_cookie);param::set_cookie('sina_servertime', $this->json_obj->servertime);param::set_cookie('sina_nonce', $this->json_obj->nonce);param::set_cookie('sina_rsakv', $this->json_obj->rsakv);//加密明文密码$this->ajax_pwd_encode();}//根据预登陆返回信息,登录function account_login() {//登录url$login_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_='.time();$this->request_headers['Content-Type'] = 'application/x-www-form-urlencoded';//登录所需数据$post_data['entry'] = 'account';$post_data['gateway'] = '1';$post_data['from'] = '';$post_data['savestate'] = '0';$post_data['useticket'] = '0';$post_data['pagerefer'] = '';$post_data['vsnf'] = '1';$post_data['su'] = param::get_cookie('sina_su');$post_data['service'] = 'sso';$post_data['servertime'] = param::get_cookie('sina_servertime');$post_data['nonce'] = param::get_cookie('sina_nonce');$post_data['pwencode'] = 'rsa2';$post_data['rsakv'] = param::get_cookie('sina_rsakv');$post_data['sp'] = $this->sp;$post_data['sr'] = '1366*768';$post_data['encoding'] = 'UTF-8';$post_data['cdult'] = '3';$post_data['domain'] = 'sina.com.cn';$post_data['prelt'] = '51';$post_data['returntype'] = 'TEXT';//登录$data = $this->request_url($login_url, $post_data, $this->request_cookie, $this->request_headers);//获取返回cookie 及 json数据list ( $header, $body ) = explode ( "/r/n/r/n", $data, 2 );//保存cookie$this->save_cookie($header);$json_login = json_decode($body);//访问返回json链接$domain_urls = $json_login->crossDomainUrlList;$i = 0;foreach ($domain_urls as $v) {$req_url = $v.'&callback=sinaSSOController.doCrossDomainCallBack&scriptId=ssoscript'.$i.'&client=ssologin.js(v1.4.15)&_='.time();$req_data = $this->request_url ( $req_url, null, $this->request_cookie, array(), 0);// list ( $header, $body ) = explode ( "/r/n/r/n", $data, 2 );// $this->save_cookie($header);$i ++;}}function save_cookie($header) {$headers = explode('/r/n', $header);foreach ($headers as $v) {$tmp = explode("/r/n", $v);foreach ($tmp as $it) {$pos = strpos($it, 'Set-Cookie');if ($pos !== false) {$cv = explode(":", $it);$this->request_cookie .= $cv[1].';';}}}}function sina_login() {//获取加密后的密码$this->sp = $_GET['sp'];//账号登录$this->account_login();//重定向到新浪通行证页面 $this->request_cookie = trim($this->request_cookie); $this->request_headers['Content-Type'] = 'text/html'; $url = 'http://login.sina.com.cn';//返回的html $html = $this->request_url($url, null, $this->request_cookie, $this->request_headers, 0);}//调用js 加密密码function ajax_pwd_encode() {echo "";echo << $v ) {$o .= "$k=" . urlencode ( $v ) . "&";}$post_data = substr ( $o, 0, - 1 );$is_post = true;}$ch = curl_init (); // 初始化curlcurl_setopt ( $ch, CURLOPT_URL, $url ); // 抓取指定网页curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); // 要求结果为字符串且输出到屏幕上if ($is_post) {curl_setopt ( $ch, CURLOPT_POST, 1 ); // post提交方式curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data ); // post数据} curl_setopt ( $ch, CURLOPT_COOKIE, $request_cookies ); // 请求cookiecurl_setopt ( $ch, CURLOPT_HEADER, $return_cookie); // 返回cookie到头curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );curl_setopt ( $ch, CURLOPT_HTTPHEADER, $request_headers );curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1);$data = curl_exec ( $ch ); // 运行curlcurl_close ( $ch );return $data;}}?>

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表