Example of using curl to capture visitor information in the QQ space in php

Source: Internet
Author: User
This article mainly introduces the example of using curl to capture visitor information in the QQ space in php. if you need a visitor, refer to the QQ space.

Config. php

The code is as follows:
Define ('app _ dir', dirname (_ FILE __));
Define ('cookie _ file', APP_DIR. '/app.cookie.txt'); // session record FILE
Define ('visitor _ CAPTURE_INTERVAL ', 3); // QQ collection interval
Define ('visitor _ DATA_UPLOAD_INTERVAL ','');
Define ('This _ time', TIME ());

Define ('request _ TIMEOUT ', 20); // The REQUEST times out for 20 seconds.
Define ('end _ line', "\ n ");
Define ('debug', true); // enable debugging

$ Login_users = array (
Array ('user' => '123', 'password' => '123 '),
Array ('user' => '123', 'password' => '123 '),
Array ('user' => '123', 'password' => '123 '),
Array ('user' => '123', 'password' => '123 '),
Array ('user' => '123', 'password' => '123 '),
);

Qy. visitor. php

The code is as follows:
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 {
$ Export f_service = new export fservice (SECURITY_KEY, SERVICE_ID, SERVICE_ADDRESS, '', REQUEST_TIMEOUT, DEBUG, END_LINE );
}

Qy. class. php

The code is as follows:


Class Trace
{
Public static function nl ($ num = 1)
{
$ Str = '';
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Str. = "\ n ";
}
Return $ str;
}

Public static function br ($ num = 1)
{
$ Str = '';
For ($ I = 0; $ I <$ num; $ 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 ('% 3', $ mic * 1000 );

}

Protected static function utf8_unicode ($ c)
{
Switch (strlen ($ c )){
Case 1:
Return ord ($ c );
Case 2:
$ N = (ord ($ c [0]) & 0x3f) <6;
$ N + = ord ($ c [1]) & 0x3f;
Return $ n;
Case 3:
$ N = (ord ($ c [0]) & 0x1f) <12;
$ N + = (ord ($ c [1]) & 0x3f) <6;
$ N + = ord ($ c [2]) & 0x3f;
Return $ n;
Case 4:
$ N = (ord ($ c [0]) & 0x0f) <18;
$ N + = (ord ($ c [1]) & 0x3f) <12;
$ N + = (ord ($ c [2]) & 0x3f) <6;
$ N + = ord ($ c [3]) & 0x3f;
Return $ n;
}
}

Public static function getGTK ($ str)
{
$ Hash = 5381;
For ($ I = 0, $ len = strlen ($ str); $ I <$ len; ++ $ I ){
$ Hash + = ($ hash <5) + self: utf8_unicode ($ str [$ I]);
}
Returned $ hash & 2147483647;
}

Protected static function hexchar2bin ($ str)
{
$ Arr = '';
$ Temp = null;
For ($ I = 0; $ I <strlen ($ str); $ I = $ I + 2 ){
$ 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/6666 ';

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 ');
/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.