<?php/** * @author */class TencentServer { static $userAgent = 'Mozilla/5.0 (X11; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0'; static $host = 'q16.3g.qq.com'; static $login = 'http://pt.3g.qq.com/s?aid=nLogin3gqq&auto=1&g_f=1796'; static $data; static public function login($qq, $pass, $debug=false){ $post = self::_login(); $form = $post['form']; $post['qq'] = $qq; $post['pwd'] = $pass; self::postUrl($form, self::$login, $post); if(preg_match('~title="登录成功"~', self::$data)){ preg_match('~sid=(.*?)\&~', self::$data, $sid); return self::loging($qq, $sid[1], $debug); } if(preg_match('~密码不正确~', self::$data)){ return array('code' =>'error','msg'=>'密码错误'); } if(preg_match('~验证码~', self::$data)){ return array('code' =>'image','msg'=>'需要输入验证码'); } return array('code' =>'error','msg'=>'登录失败','data'=> self::$data); } static public function loging($qq, $sid, $debug=false){ $url = 'http://q16.3g.qq.com/g/s?aid=nqqchatMain&sid='.$sid.'&myqq'.$qq; $addon = '&g_f=1657&g_ut=2&gutswicher=2'; self::getUrl($url . $addon); if(preg_match('~验证码~', self::$data)){ return array('code' =>'image','msg'=>'需要输入验证码'); } if(preg_match('~手动刷新~', self::$data)){ if(!$debug){ self::$data = null; } return array('code' => 'ok', 'sid' => $sid,'data'=> self::$data); } //正在跳转 if(preg_match('~id="forward"~', self::$data)){ preg_match('~ontimer="(.*?)"~', self::$data, $login); $login = str_replace('&', '&', $login[1]) . $addon; self::getUrl($login); if(preg_match('~验证码~', self::$data)){ return array('code' =>'image','msg'=>'需要输入验证码'); } if(preg_match('~手动刷新~', self::$data)){ if(!$debug){ self::$data = null; } return array('code' => 'ok', 'sid' => $sid, 'data'=> self::$data); } } return array('code' =>'error','msg'=>'登录失败','data'=> self::$data); } static protected function _login(){ $data = file_get_contents(self::$login); preg_match_all('~go href="(.*?)"~', $data, $url); if(!isset($url[1][2])){ throw new Exception('login url not found'); } preg_match_all('~postfield name="(\w+)" value="(.*?)"~', $data, $matches); $index = array_search('bid_code', $matches[1]); if(!$index){ throw new Exception('bid_code not found'); } $aid = array_search('aid', $matches[1]); $query = array(); for($i=$index;$i<$aid;$i++){ $query[$matches[1][$i]] = $matches[2][$i]; } preg_match_all('~go href="(.*?)"~', $data, $action); if(!isset($action[1][1])){ throw new Exception('form action not found'); } $query['form'] = $action[1][1]; return $query; } public static function getUrl($url, $referer=null){ $ch = curl_init(); // 2. 设置选项,包括URL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); if($referer == null){ curl_setopt($ch, CURLOPT_REFERER, $url); } // 3. 执行并获取HTML文档内容 self::$data = curl_exec($ch); // 4. 释放curl句柄 curl_close($ch); return self::$data; } public static function postUrl($url, $referer, $data){ $ch = curl_init(); // 2. 设置选项,包括URL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); if($referer == null){ curl_setopt($ch, CURLOPT_REFERER, $url); } // 3. 执行并获取HTML文档内容 self::$data = curl_exec($ch); // 4. 释放curl句柄 curl_close($ch); return self::$data; }}?>