Rumah >pembangunan bahagian belakang >tutorial php >php 新浪通行证登录 新浪微博登录 模拟登录 (浏览器版) 2016
由于需要项目需要管理微博平台内容,集成登录微博管理平台功能,所以在抓取网页基础上探索了一下浏览器模拟登录,已经实现登录功能,采用JQuery和php结合的方式实现的,其中需要注意跨域访问问题,代码如下:
<?php /** * tom 2016年4月12日10:37:08 模拟微博登录 */class login_weibo2 { // 微博用户名称密码 private $username = ''; private $password = ''; //请求cookie private $request_cookie = ''; //预登陆返回json private $json_obj = null; //请求头 private $request_headers = array ( 'Host' => '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\-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后变量重置,所以存到cookie param::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()); 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].';'; $_COOKIE[$cv[0]] = $cv[1]; setcookie($cv[0], $cv[1], time()+60*60*24*30); param::set_cookie($cv[0], $cv[1]); } } } } //根据预登陆返回信息,登录 function browser_login() { //登录url $login_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_='.time(); //登录所需数据 $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'; echo ""; echo ""; echo "