Home >Backend Development >PHP Tutorial >php使用curl抓取qq空间的访客信息示例_PHP
QQ空间
config.php
复制代码 代码如下:
define('APP_DIR', dirname(__FILE__));
define('COOKIE_FILE', APP_DIR . '/app.cookie.txt'); //会话记录文件
define('VISITOR_CAPTURE_INTERVAL', 3); //QQ采集间隔
define('VISITOR_DATA_UPLOAD_INTERVAL', '');
define('THIS_TIME', time());
define('REQUEST_TIMEOUT', 20); //请求超时20秒
define('END_LINE', "\n");
define('DEBUG', true); //开启调试
$login_users = array(
array('user' => '2064556526', 'password' => '909124951'),
array('user' => '483258700', 'password' => '909124951'),
array('user' => '1990270522', 'password' => '909124951'),
array('user' => '2718711637', 'password' => '909124951'),
array('user' => '2841076562', 'password' => '909124951'),
);
qy.visitor.php
复制代码 代码如下:
include('./config.php');
include(APP_DIR . '/qy.visitor.php');
$sessions = array();
$user = $login_users[array_rand($login_users)];
$visitor_capture = new QQVisitorCapture($user['user'], $user['password'], COOKIE_FILE, REQUEST_TIMEOUT, DEBUG, END_LINE);
$visitors = $visitor_capture->getVisitorInfo();
if (empty($visitors)) {
$this->clearCookies(true);
} else {
$cckf_service = new CCKFService(SECURITY_KEY,SERVICE_ID,SERVICE_ADDRESS,'', REQUEST_TIMEOUT, DEBUG, END_LINE);
}
qy.class.php
复制代码 代码如下:
class Trace
{
public static function nl($num = 1)
{
$str = '';
for ($i = 0; $i $str .= "\n";
}
return $str;
}
public static function br($num = 1)
{
$str = '';
for ($i = 0; $i $str .= "
";
}
return $str;
}
public static function write($content, $end_line, $title = null)
{
$close = '^^^^^^^^^^^^^^^^^';
if ($title) {
$start = '--------' . $title . '---------';
} else {
$start = '-----------------';
}
echo $start . $end_line;
if (is_array($content)) {
print_r($content);
echo $end_line;
} else {
echo $content;
echo $end_line;
}
if (empty($content)) {
echo $end_line;
} else {
echo $close . $end_line;
}
}
}
class Utils
{
public static function getMicroTime()
{
list($mic, $time) = explode(" ", microtime());
return intval($time) + floatval(sprintf('%.3f', $mic));
}
public static function getUTCMilliseconds()
{
return round(rand(1, 9) / 10 * 2147483647) * round(1, 999) % 10000000000;
}
public static function decodeURIComponent($content)
{
return urldecode(preg_replace("/\\\\x([0-9a-z]{2,3})/i", "%$1", $content));
}
public static function jsRandom()
{
list($mic, $time) = explode(" ", microtime());
return $mic;
}
function loginJsTime()
{
list($mic, $time) = explode(" ", microtime());
return $time . sprintf('%3d', $mic * 1000);
}
protected static function utf8_unicode($c)
{
switch (strlen($c)) {
case 1:
return ord($c);
case 2:
$n = (ord($c[0]) & 0x3f) $n += ord($c[1]) & 0x3f;
return $n;
case 3:
$n = (ord($c[0]) & 0x1f) $n += (ord($c[1]) & 0x3f) $n += ord($c[2]) & 0x3f;
return $n;
case 4:
$n = (ord($c[0]) & 0x0f) $n += (ord($c[1]) & 0x3f) $n += (ord($c[2]) & 0x3f) $n += ord($c[3]) & 0x3f;
return $n;
}
}
public static function getGTK($str)
{
$hash = 5381;
for ($i = 0, $len = strlen($str); $i $hash += ($hash }
return $hash & 2147483647;
}
protected static function hexchar2bin($str)
{
$arr = '';
$temp = null;
for ($i = 0; $i $arr .= "\\x" . substr($str, $i, 2);
}
eval('$temp="' . $arr . '";');
return $temp;
}
protected static function getUid($uid)
{
$temp = null;
eval('$temp="' . $uid . '";');
return $temp;
}
public static function getEncryption($password, $uin, $vcode)
{
$uin = self::getUid($uin);
$str1 = self::hexchar2bin(strtoupper(md5($password)));
$str2 = strtoupper(md5($str1 . $uin));
return strtoupper(md5($str2 . strtoupper($vcode)));
}
}
class CookieFileExtract
{
protected $cookie_file;
protected $cookie_list;
protected function __construct($cookie_file)
{
$this->cookie_file = $cookie_file;
$this->cookie_list = $this->extractFile();
}
protected function isValidateCookieFile()
{
if ($this->cookie_file && file_exists($this->cookie_file)) {
return true;
} else {
return false;
}
}
protected function extractFile()
{
$cookie_list = array();
if ($this->isValidateCookieFile($this->cookie_file)) {
$content = file($this->cookie_file);
if (is_array($content)) {
foreach ($content as $line) {
$line = trim($line);
if (strlen($line) > 0 && $line[0] != '#') {
$cookie = (preg_split("/\s+/", $line));
if (count($cookie) == 7) {
$cookie_list[$cookie[5]] = $cookie[6];
} else {
$cookie_list[$cookie[5]] = '';
}
}
}
}
}
return $cookie_list;
}
protected function buildCookieStr($cookies)
{
$arr = array();
if (is_array($cookies)) {
foreach ($cookies as $k => $cookie) {
$line = $cookie['domain'] . "\t" . "TRUE" . "\t" . $cookie['path'] . "\t" . "FALSE" . "\t" . $cookie['expires'] . "\t" . $k . "\t" . $cookie['value'];
$arr[] = $line;
}
}
return $arr;
}
protected function __setCookies($cookies)
{
$new_line = array();
if (is_array($cookies)) {
if ($this->isValidateCookieFile($this->cookie_file)) {
$content = file($this->cookie_file);
if (is_array($content)) {
foreach ($content as $line) {
$line = trim($line);
if (strlen($line) > 0 && $line[0] != '#') {
$cookie = (preg_split("/\s+/", $line));
if (!in_array($cookie[5], $cookies)) {
$new_line[] = $line;
}
} else {
$new_line[] = $line;
}
}
}
}
file_put_contents($this->cookie_file, implode("\n", array_merge($new_line, $this->buildCookieStr($cookies))));
}
}
protected function __getAllCookies($refresh = false)
{
if ($refresh) {
$this->cookie_list = $this->extractFile();
}
return $this->cookie_list;
}
protected function __getCookie($cookie_name, $refresh = false)
{
$cookie_list = $this->__getAllCookies($refresh);
if (is_array($cookie_list) && array_key_exists($cookie_name, $cookie_list)) {
return $cookie_list[$cookie_name];
} else {
return null;
}
}
protected function __clearAllCookies()
{
$this->cookie_list = null;
@unlink($this->cookie_file);
}
}
class BaseRequest extends CookieFileExtract
{
protected $curl_instance;
protected $request_timeout;
protected $debug;
protected $end_line;
protected $user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) Gecko/20100101 Firefox/26.0';
protected function __construct($cookie_file, $request_timeout, $debug, $end_line)
{
parent::__construct($cookie_file);
$this->request_timeout = $request_timeout;
$this->debug = $debug;
$this->end_line = $end_line;
$this->initInstance();
}
protected function initInstance()
{
$this->curl_instance = curl_init();
if ($this->isValidateCookieFile()) {
curl_setopt($this->curl_instance, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($this->curl_instance, CURLOPT_COOKIEFILE, $this->cookie_file);
}
curl_setopt($this->curl_instance, CURLOPT_TIMEOUT, $this->request_timeout);
curl_setopt($this->curl_instance, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl_instance, CURLOPT_HEADER, 1);
curl_setopt($this->curl_instance, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curl_instance, CURLOPT_SSL_VERIFYHOST, 0);
curl_exec($this->curl_instance);
}
function setCookies($cookies)
{
$this->closeInstance();
$this->__setCookies($cookies);
$this->initInstance();
}
protected function getAllCookies($refresh = false)
{
$this->closeInstance();
$cookies = $this->__getAllCookies($refresh);
$this->initInstance();
return $cookies;
}
protected function clearAllCookies($refresh = false)
{
$this->closeInstance();
$this->__clearAllCookies();
if ($refresh) {
$this->initInstance();
}
}
protected function getCookie($name, $refresh = false)
{
$this->closeInstance();
$cookie = $this->__getCookie($name, $refresh);
$this->initInstance();
return $cookie;
}
protected function getRequestInstance()
{
return $this->curl_instance;
}
protected function closeInstance()
{
if (is_resource($this->curl_instance)) {
curl_close($this->curl_instance);
}
}
protected function resetInstance()
{
$this->closeInstance();
@unlink($this->cookie_file);
$this->initInstance();
}
protected function requestExec($option)
{
curl_setopt_array($this->getRequestInstance(), $option);
//if ($this->debug) {
// $result = curl_exec($this->getRequestInstance());
// Trace::write($result, $this->end_line, 'request output');
/