Home > Article > Backend Development > [分享]模拟新浪微博自动登陆,该如何处理
[分享]模拟新浪微博自动登陆
博客原文地址: http://blog.csdn.net/lgg201/article/details/8050606
<br> <?php <br /> /**<br> * 功能: 模拟新浪微博登陆<br> * 用途: 模拟用户登陆, 以便进行后续操作, 比如自动化的控制自己的新浪app刷新某些数据<br> * 注意事项: <br> * 1. 需要安装nodejs<br> * 2. 需要下载新浪的加密js文件, 请到新浪登陆页查看网络请求自己下载最新版本(我当时用的: http://js.t.sinajs.cn/t35/miniblog/static/js/sso.js?version=e482ef2bbdaa8bc2)<br> * 3. 对新浪加密js文件进行修改, 以便让nodejs可以运行它<br> * 1) 在文件前面增加下面内容<br> var window = {<br> location : {<br> hash : '', <br> host : 'weibo.com', <br> hostname : 'weibo.com', <br> href : 'http://weibo.com/', <br> pathname : '/', <br> port : '', <br> protocol : 'http:', <br> search : ''<br> }, <br> navigator : {<br> appCodeName : 'Mozilla', <br> appName : 'Netscape', <br> appVersion : '5.0 (Macintosh)', <br> buildID : '20120713134347', <br> cookieEnabled : true, <br> doNotTrack : 'unspecified', <br> language : 'en-US'<br> }<br> };<br> var location = window.location;<br> var navigator = window.navigator;<br> * 2) 在文件后面增加下面内容<br> var argv = process.argv.splice(2);<br> <br> var pubkey = argv[0],<br> servertime = argv[1],<br> nonce = argv[2],<br> password = argv[3];<br> <br> var RSAKey = new sinaSSOEncoder.RSAKey();<br> RSAKey.setPublic(pubkey, '10001');<br> password = RSAKey.encrypt([servertime, nonce].join("\t") + "\n" + password); <br> console.log(password);<br> process.exit();<br> * 4. 修改encode_password函数中的nodejs程序路径和修改后的新浪js文件路径<br> * 5. 修改用户名密码<br> * author: selfimpr<br> * blog: http://blog.csdn.net/lgg201<br> * mail: lgg860911@yahoo.com.cn<br> */<br> <br> define('REQUEST_METHOD_GET', 'GET');<br> define('REQUEST_METHOD_POST', 'POST');<br> define('REQUEST_METHOD_HEAD', 'HEAD');<br> <br> define('COOKIE_FILE', '/tmp/sina.login.cookie');<br> <br> function curl_switch_method($curl, $method) {<br> switch ( $method) {<br> case REQUEST_METHOD_POST:<br> curl_setopt($curl, CURLOPT_POST, TRUE);<br> break;<br> case REQUEST_METHOD_HEAD:<br> curl_setopt($curl, CURLOPT_NOBODY, TRUE);<br> break;<br> case REQUEST_METHOD_GET:<br> default:<br> curl_setopt($curl, CURLOPT_HTTPGET, TRUE);<br> break;<br> }<br> }<br> function curl_set_headers($curl, $headers) {<br> if ( empty($headers) ) return ;<br> if ( is_string($headers) ) <br> $headers = explode("\r\n", $headers);<br> #类型修复<br> foreach ( $headers as &$header ) <br> if ( is_array($header) ) <br> $header = sprintf('%s: %s', $header[0], $header[1]);<br> curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);<br> }<br> function curl_set_datas($curl, $datas) {<br> if ( empty($datas) ) return ;<br> curl_setopt($curl, CURLOPT_POSTFIELDS, $datas);<br> }<br> function curl_request($url, $method = REQUEST_METHOD_GET, $datas = NULL, $headers = NULL) {<br> static $curl;<br> if ( !$curl )<br> $curl = curl_init();<br> curl_switch_method($curl, $method);<br> curl_setopt($curl, CURLOPT_URL, $url);<br> curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);<br> curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); <div class="clear"> </div>