BDUSS在线获取程序

  • 2016-09-12
  • 0
  • 3

以前一直用javascript获取BDUSS,但是最近此方法经常失效。同时博主最近积极配合治疗,懒癌得到初步控制,所以写了个BDUSS在线获取程序。
为了防止此程序被恶意利用,此程序仅部分开源。
程序源码:http://code.ikay.me/index.php?share/folder&user=ikay&sid=OqPolVLT
在线获取BDUSS程序地址:
http://www.xsico.cn/bduss/index.php
部分百度操作类:

class BaiduUtil{
public $useZlib       = FALSE;
public $returnThis    = FALSE;
public $lastFetch     = array();
public $lastReturn    = array();
public $lastformData  = array();

protected $un         = '';
protected $uid        = '';
protected $tbs        = '';
protected $bduss      = '';
protected $cookie     = '';
protected $client     = array();
protected $formData   = array();
protected $forumPages = array();

public function __construct($cookie = NULL, $userinfo = array(), $client = NULL){
if(!is_null($cookie)){
$cookie = trim($cookie);
$temCookieHasBduss = stripos($cookie,'bduss=');
$temCookieHasSemicolon = stripos($cookie,';');
if($temCookieHasBduss === FALSE &&  $temCookieHasSemicolon === FALSE){
$this->bduss = $cookie;
}elseif($temCookieHasBduss !== FALSE && $temCookieHasSemicolon === FALSE){
$this->bduss = substr($cookie,6);
}elseif(preg_match('/bduss\s?=\s?([^ ;]*)/i', $cookie, $matches)){
$this->bduss = $matches[1];
}else{
throw new Exception('请输入合法的cookie',-99);
}
$this->cookie = $this->buildFullCookie();
}
if(is_null($client)){
$this->client = self::getClient();
}else{
$this->client = $client;
}
if(isset($userinfo['un'])) $this->un = $userinfo['un'];
if(isset($userinfo['uid'])) $this->uid = $userinfo['uid'];
}

protected function fetch($url,$mobile = TRUE,$usecookie = TRUE){
$ch = curl_init($url);
if($mobile === TRUE){
$common_data = array(
'from'        => 'baidu_appstore',
'stErrorNums' => '0',
'stMethod'    => '1',
'stMode'      => '1',
'stSize'      => rand(50,2000),
'stTime'      => rand(50,500),
'stTimesNum'  => '0',
'timestamp'   => time() . self::random(3,TRUE)
);
$predata = $this->client + $this->formData + $common_data;
ksort($predata);
$this->formData = array();
if($usecookie === TRUE){
$this->formData['BDUSS'] = $this->bduss;
}
$this->formData += $predata;
$sign_str = '';
foreach($this->formData as $key=>$value)
$sign_str .= $key . '=' . $value;
$sign = strtoupper(md5($sign_str . 'tiebaclient!!!'));
$this->formData['sign'] = $sign;
$http_header = array(
'User-Agent: BaiduTieba for Android 6.0.1',
'Content-Type: application/x-www-form-urlencoded',
'Host: c.tieba.baidu.com',
'Connection: Keep-Alive'
);
if($this->useZlib === TRUE) $http_header[] = 'Accept-Encoding: gzip';
}else{
$http_header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.3; rv:29.0) Gecko/20100101 Firefox/29.0',
'Connection: Keep-Alive'
);
curl_setopt($ch,CURLOPT_COOKIE,$this->cookie);
}
curl_setopt($ch,CURLOPT_HTTPHEADER,$http_header);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($this->formData));
$res_json = curl_exec($ch);
curl_close($ch);
if(empty($res_json)) throw new Exception('网络连接失败',-10);
if($this->useZlib === TRUE) $res_json = gzdecode($res_json);
$result = @json_decode($res_json,TRUE);
if($mobile === TRUE){
if(!array_key_exists('error_code',$result)) throw new Exception('未收到正确数据',-11);
if(!empty($result['anti']['tbs'])) $this->tbs = $result['anti']['tbs'];
if(!empty($result['user']['id']))  $this->uid = $result['user']['id'];
if(!empty($result['user']['name'])) $this->un = $result['user']['name'];
}
$this->last_formData = $this->formData;
$this->formData      = array();
$this->lastFetch      = $result;
return $result;
}

评论

CC共享许可协议:BY-NC-SA 4.0 - 友情链接 - Theme by Qzhai