curl模擬登陸新浪微博

   這幾天要作個獲取新浪微博@個人信息, 又不用第三方登陸,因此只能經過模擬登陸來獲取信息,研究的一下發現直接模擬登陸微博比較困難,驗證的算法比較複雜,因而繞道經過登陸新浪通行證後來獲取cookie 來獲取信息,代碼以下。php

   

<?php
header('Content-type:text/html;charset=utf-8');

$loginUrl = 'https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543';

$cookie_file1 = dirname(__FILE__).'/cookie/cookie1.txt';
$cookie_file2 = dirname(__FILE__).'/cookie/cookie2.txt';
$cookie_file3 = dirname(__FILE__).'/cookie/cookie3.txt';

$u = '此處通行證賬號';
$p = '此處通行證密碼';

$username = base64_encode($u);
$password = $p;

//登陸新浪通行證
$loginData['entry'] = 'sso';
$loginData['gateway'] = '1';
$loginData['from'] = 'null';
$loginData['savestate'] = '30';
$loginData['useticket'] = '0';
$loginData['pagerefer'] = '';
$loginData['vsnf'] = '1';
$loginData['su'] = base64_encode($u);
$loginData['service'] = 'sso';
$loginData['sp'] = $password;
$loginData['sr'] = '1920*1080';
$loginData['encoding'] = 'UTF-8';
$loginData['cdult'] = '3';
$loginData['domain'] = 'sina.com.cn';
$loginData['prelt'] = '0';
$loginData['returntype'] = 'TEXT';

$login = json_decode(loginPost($loginUrl,$loginData),true);

//獲取微博cookie
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$login['crossDomainUrlList'][0]);    
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);    
curl_setopt($ch,CURLOPT_COOKIEFILE, $cookie_file1);    
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file2);    
$return = curl_exec($ch);
curl_close($ch);

//經過獲取的cookie 登陸微博, 自動跳轉
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://weibo.com/at/weibo?topnav=1&wvr=5");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
curl_setopt($ch,CURLOPT_COOKIEFILE, $cookie_file2);    
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file3);
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );    
$return = curl_exec($ch);
//$info = curl_getinfo($ch);
curl_close($ch);
echo $return;


function loginPost($url,$data){
        global $cookie_file1 ;
        //echo $cookie_file ;exit;
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);    
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);    
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);        
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file1);
        $return = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        return $return;
}
相關文章
相關標籤/搜索