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