PHP CURL 模擬Sina微博登陸

<?php 
/*
 * PHP CURL 模擬Sina微博登陸
 * @access sina_login_curl.php
 * @create 2012-09-28
 */

# 配置項目路徑
define('APPLICATION_PATH', dirname(__FILE__));
define('COOKIE_PATH', APPLICATION_PATH);

# 獲取時間戳
define('TIMESTAMP', $_SERVER['REQUEST_TIME']);

# 調試,會在當前文件夾下面建立LOG文件
define('DEBUG', false);

# 模擬Sina帳號密碼
$username = "";
$password = "";

$weiboLogin = new weiboLogin( $username, $password );
exit($weiboLogin->showTestPage('http://weibo.com/at/comment'));

class weiboLogin{
private $cookiefile;
private $username;
private $password;

function __construct($username, $password) {
($username == '' || $password == '') && exit("請填寫用戶名密碼");

$this->cookiefile = COOKIE_PATH . '/cookie_sina_' . substr(base64_encode($username), 0, 10);
$this->username = $username;
$this->password = $password;
}

/**
* CURL請求
* @param String $url 請求地址
* @param Array $data 請求數據
*/
function curlRequest($url, $data = false) {
$ch = curl_init();

$option = array(
CURLOPT_URL => $url, 
CURLOPT_HEADER => 0, 
CURLOPT_HTTPHEADER => array('Accept-Language: zh-cn','Connection: Keep-Alive','Cache-Control: no-cache'), 
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1", 
CURLOPT_FOLLOWLOCATION => TRUE, 
CURLOPT_MAXREDIRS => 4, 
CURLOPT_RETURNTRANSFER => TRUE, 
CURLOPT_COOKIEJAR => $this->cookiefile, 
CURLOPT_COOKIEFILE => $this->cookiefile
);

if ($data) {
$option[CURLOPT_POST] = 1;
$option[CURLOPT_POSTFIELDS] = $data;
}

curl_setopt_array($ch, $option);
$response = curl_exec($ch);

if (curl_errno($ch) > 0) {
exit("CURL ERROR:$url " . curl_error($ch));
}
curl_close($ch);
return $response;
}

function doSinaLogin() {
# Step 1: Get tickit
$preLoginData = $this->curlRequest('http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=' .
base64_encode($this->username) . '&client=ssologin.js(v1.3.16)');
preg_match('/sinaSSOController.preloginCallBack\((.*)\)/', $preLoginData, $preArr);
$jsonArr = json_decode($preArr[1], true);

$this->debug('debug_1_Tickit', $preArr[1]);

if (is_array($jsonArr)) {
# Step 2: Do Certification
$postArr = array(
'entry' => 'weibo', 
'gateway' => 1, 
'from' => '', 
'vsnval' => '', 
'savestate' => 7, 
'useticket' => 1, 
'ssosimplelogin' => 1, 
'su' => base64_encode(urlencode($this->username)), 
'service' => 'miniblog', 
'servertime' => $jsonArr['servertime'], 
'nonce' => $jsonArr['nonce'], 
'pwencode' => 'wsse', 
'sp' => sha1(sha1(sha1($this->password)) . $jsonArr['servertime'] . $jsonArr['nonce']), 
'encoding' => 'UTF-8', 
'url' => 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 
'returntype' => 'META'
);

$loginData = $this->curlRequest('http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)', $postArr);

$this->debug('debug_2_Certification_raw', $loginData);

# Step 3: SSOLoginState
if ($loginData) {
$matchs = $loginResultArr = array();
preg_match('/replace\(\'(.*?)\'\)/', $loginData, $matchs);

$this->debug('debug_3_Certification_result', $matchs[1]);

$loginResult = $this->curlRequest($matchs[1]);
preg_match('/feedBackUrlCallBack\((.*?)\)/', $loginResult, $loginResultArr);

$userInfo = json_decode($loginResultArr[1], true);

$this->debug('debug_4_UserInfo', $loginResultArr[1]);
} else {
exit('Login sina fail');
}
} else {
exit('Server tickit fail');
}
}

# Test Login
function showTestPage($url) {
$file_holder = $this->curlRequest($url);

// 若是未登陸狀況, 登陸後再嘗試
$isLogin = strpos($file_holder, 'class="user_name"');
if (! $isLogin) {
unset($file_holder);
$this->doSinaLogin();
$file_holder = $this->curlRequest($url);
}
return $file_holder ;
}

# Debug
function debug($file_name, $data) {
if (DEBUG) {
file_put_contents($file_name . '.txt', $data);
}
}
}
相關文章
相關標籤/搜索