Analog Sina Weibo automatic landing _php tutorial

Source: Internet
Author: User
See the program notes for more information.
[PHP]
/**
* Function: Analog Sina Weibo landing
* Use: Simulate user login for follow-up operation, such as automating the control of your Sina app to refresh some data
* Precautions:
* 1. Need to install Nodejs
* 2. Need to download Sina's encrypted JS file, please go to the Sina Landing page to view the Web request to download the latest version (I was using: HTTP://JS.T.SINAJS.CN/T35/MINIBLOG/STATIC/JS/SSO.JS?VERSION=E482EF2BBDAA8BC2)
* 3. Make changes to the Sina encryption JS file so that Nodejs can run it
* 1) Add the following in front of 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: ' 20120713134347 ',
Cookieenabled:true,
Donottrack: ' Unspecified ',
Language: ' en-US '
}
};
var location = window.location;
var navigator = Window.navigator;
* 2) Add the following content to 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, ' 10001 ');
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. Modify User name password
* 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)) {
Error_log (sprintf ("%10d\t%s\n", $errno, Curl_error ($curl)), 3, ' php://stderr ');
return FALSE;
}
return $response;
}

function Get_js_timestamp () {
return time () * + 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%s%s%s%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) {
#这里是要用nodejs执行新浪的js文件
$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 (
' Prelogintime ' = 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 ',
' RSAKV ' = $info [' rsakv '],
' 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 ']);

http://www.bkjia.com/PHPjc/477947.html www.bkjia.com true http://www.bkjia.com/PHPjc/477947.html techarticle See the program notes for more information. [PHP]? PHP/** * Features: Analog Sina Weibo landing * Use: Simulation user login, in order to follow up, such as automated control of their own Sina app Refresh ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.