Simulate automatic login to Sina Weibo. For details, see program Comments. [php]? Php *** function: simulate Sina Weibo login * purpose: simulate user login for subsequent operations. for example, to automatically control your Sina app refresh details, see program notes.
[Php]
/**
* Function: simulate Sina Weibo login
* Purpose: simulate user login for subsequent operations. for example, you can automatically control your Sina app to refresh certain data.
* Note:
* 1. nodejs needs to be installed
* 2. need to download Sina's encrypted js file, please go to Sina login page to view the network request to download the latest version (I was using: http://js.t.sinajs.cn/t35/miniblog/static/js/sso.js? Version = e482ef2bbdaa8bc2)
* 3. modify the Sina encrypted js file so that nodejs can run it.
* 1) add the following content before the file
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: '000000 ',
CookieEnabled: true,
DoNotTrack: 'unspecified ',
Language: 'En-us'
}
};
Var location = window. location;
Var navigator = window. navigator;
* 2) add the following content after the file
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, '20140901 ');
Password = RSAKey. encrypt ([servertime, nonce]. join ("\ t") + "\ n" + password );
Console. log (password );
Process. exit ();
* 4. modify the nodejs program path in the encode_password function and the modified Sina js file path.
* 5. change the user name and password
* Author: selfimip
* 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. cooker ');
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 );
# Type repair
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 )){
Error_log (sprintf ("% 10d \ t % s \ n", $ errno, curl_error ($ curl), 3, 'php: // stderr ');
Return FALSE;
}
Return $ response;
}
Function get_js_timestamp (){
Return time () * 1000 + rand (0,999 );
}
Function http_build_query_no_encode ($ datas ){
$ R = array ();
Foreach ($ datas as $ k => $ v)
$ R [] = $ k. '='. $ v;
Return implode ('&', $ r );
}
Function makeUrl ($ url, $ info, $ encode = TRUE ){
If (! Is_array ($ info) | emptyempty ($ info) return $ url;
$ Components = parse_url ($ url );
If (array_key_exists ('query', $ components ))
$ Query = parse_str ($ components ['query']);
Else
$ Query = array ();
If (is_string ($ info) $ info = parse_str ($ info );
$ Query = array_merge ($ query, $ info );
$ Query = $ encode
? Http_build_query ($ query)
: Http_build_query_no_encode ($ query );
$ Components ['scheme '] = array_key_exists ('scheme', $ components)
? $ Components ['scheme '].': //'
:'';
$ Components ['user'] = array_key_exists ('user', $ components)
? $ Components ['user']. ':'. $ components [HTTP_URL_PASS]. '@'
:'';
$ Components ['host'] = array_key_exists ('host', $ components)
? $ Components ['host']
:'';
$ Components ['port'] = array_key_exists ('port', $ components)
? ':'. $ Components ['port']
:'';
$ Components ['path'] = array_key_exists ('path', $ components)
? '/'. Ltrim ($ components ['path'], '/')
:'';
$ Components ['query'] = $ query
? '? '. $ Query
:'';
$ Components ['fragment '] = array_key_exists ('fragment', $ components)
? '#'. $ Components ['fragment ']
:'';
Return sprintf ('% s % s', $ components ['scheme'], $ components ['user'], $ components ['host'],
$ Components ['port'], $ components ['path'],
$ Components ['query'], $ components ['fragment ']);
}
Function encode_username ($ username ){
Return base64_encode (urlencode ($ username ));
}
Function encode_password ($ pub_key, $ password, $ servertime, $ nonce ){
# Use nodejs to execute the Sina js file.
$ Response = '/usr/local/node. js-0.8.8/bin/node sina. js "$ pub_key" $ servertime "" $ nonce "" $ password "';
Return substr ($ response, 0, strlen ($ response)-1 );
}
Function main_page (){
Return curl_request ('Weibo. com ');
}
Function prepare_login_info (){
$ Time = get_js_timestamp ();
$ Url = makeUrl ('http: // login.sina.com.cn/sso/prelogin.php', array (
'Entry '=> 'sso ',
'Callback' => 'sinassocontroller. prelogincallback ',
'Su' => encode_username ('undefined '),
'Rsakt '=> 'mod ',
'Client' => 'ssologin. js (v1.4.2 )',
'_' => $ Time,
), FALSE );
$ Response = curl_request ($ url );
$ Length = strlen ($ response );
$ Left = 0;
$ Right = $ length-1;
While ($ left <$ length)
If ($ response [$ left] = '{') break;
Else $ left ++;
While ($ right> 0)
If ($ response [$ right] = '}') break;
Else $ right --;
$ Response = substr ($ response, $ left, $ right-$ left + 1 );
Return array_merge (json_decode ($ response, TRUE), array (
& Apos; preloginTime & apos; = & apos; max (get_js_timestamp ()-$ time, 100 ),
));
}
Function login ($ info, $ username, $ password ){
$ Feedbackurl = makeUrl ('http: // weibo.com/ajaxlogin.php', array (
'Framelogin' => 1,
'Callback' => 'parent. sinaSSOController. feedbackurlcallback ',
));
$ Datas = array (
'Encoding' => 'utf-8 ',
'Entry '=> 'Weibo ',
'From' => '',
'Gateway' => 1,
'Nonce '=> $ info ['nonce'],
'Prelt '=> $ info ['prelogintime'],
'Pwencode' => 'rsa2 ',
'Returntype' => 'meta ',
'Rsak' => $ info ['rsak'],
'Savestate' => 7,
'Servertime' => $ info ['servertime'],
'Service' => 'miniblog ',
'Sp '=> encode_password ($ info ['pubkey'], $ password, $ info ['servertime'], $ info ['nonce']),
'Ssosimplelogin' => 1,
'Su' => encode_username ($ username ),
'URL' => $ feedbackurl,
'Useticket '=> 1,
'Vsnf '=> 1,
);
$ Url = makeUrl ('http: // login.sina.com.cn/sso/login.php', array (
'Client' => 'ssologin. js (v1.4.2 )',
), FALSE );
$ Response = curl_request ($ url, REQUEST_METHOD_POST, $ datas );
$ Sign = 'location. replace (\'';
$ Response = substr ($ response, strpos ($ response, $ sign) + strlen ($ sign ));
$ Location = substr ($ response, 0, strpos ($ response ,'\''));
$ Response = curl_request ($ location );
$ Length = strlen ($ response );
$ Left = 0;
$ Right = $ length-1;
While ($ left <$ length)
If ($ response [$ left] = '{') break;
Else $ left ++;
While ($ right> 0)
If ($ response [$ right] = '}') break;
Else $ right --;
$ Response = substr ($ response, $ left, $ right-$ left + 1 );
Return json_decode ($ response, true );
}
$ Info = prepare_login_info ();
$ Info = login ($ info, 'username', 'password ');
Echo curl_request ('http: // weibo.com/u/'. $ info ['userinfo'] ['uniqueid']. $ info ['userinfo'] ['userdomain ']);
Example. [php]? Php/*** function: simulate Sina Weibo login * purpose: simulate user login for subsequent operations, such as automatic control of Sina app refresh...