Friend Gateway's PHP code related to QQ (excellent information on QQ)

Source: Internet
Author: User
This article mainly introduces the PHP code related to QQ used by friend Gateway (excellent information about QQ). This article provides the implementation code directly. if you need it, refer to the following code:


<?
/***************************************

****************************************/
Error_reporting (E_ALL ^ E_NOTICE );

Require_once ('http. inc. php ');
Require_once ('class. Chinese. php ');


// Success 2xx
Define ('qq _ return_success', 200 );
Define ('qq _ login_success', 201 );
Define ('qq _ LIST_NONE ', 202 );
Define ('qq _ ADDTOLIST_SUCCESS ', 203 );
Define ('qq _ REPLYADD_SUCCESS ', 204 );
Define ('qq _ getmsg_none', 205 );

// Warning 3xx
Define ('qq _ ADDTOLIST_NEEDAUTH ', 300 );
Define ('qq _ ADDTOLIST_REFUSE ', 301 );
Define ('qq _ ADDTOLIST_UNKNOWN ', 302 );

// Failure 4xx
Define ('qq _ RETURN_FAILED ', 400 );
Define ('qq _ LIST_ERROR ', 401 );
Define ('qq _ GETMSG_ERROR ', 402 );

// Online status
Define ('qq _ STATUS_ONLINE ', 10 );
Define ('qq _ status_offline', 20 );
Define ('qq _ STATUS_BUSY ', 30 );

// Blood type
$ QQ_DATA_BT = array
(
=> '',
=> 'A ',
=> 'B ',
=> 'O type ',
=> 'AB type ',
=> 'Others'
);

// Constellation
$ QQ_DATA_CO = array
(
=> '',
=> 'Aquarius ',
=> 'Pisces ',
=> 'Sheepse ',
=> 'Taurus ',
=> 'Gemins ',
=> 'Loan ',
=> 'Leo ',
=> 'Virgo ',
=> 'Libra ',
=> 'Scorpio ',
=> 'Sagittarius ',
=> 'Capricorn'
);

// Chinese Zodiac
$ QQ_DATA_SH = array
(
=> '',
=> 'Rat ',
=> 'Niu ',
=> 'Hu ',
=> Rabbit ',
=> 'Long ',
=> 'Snake ',
=> 'Ma ',
=> 'Yang ',
=> 'Monkey ',
=> 'Chicken ',
=> 'Dog ',
=> 'Pig'
);

// Gender
$ QQ_DATA_SX = array
(
=> 'Male ',
=> Female'
);

Class QQClient
{
Var $ uin;
Var $ pwd;

Var $ server = 'kconn .tencent.com ';
// Standby: 219.133.51.11
Var $ port = 21001;
// Backup: 8000
Var $ httpclient;
Var $ chs = NULL;

Function QQClient ($ uin, $ pwd)
{
$ This-> uin = $ uin;
$ This-> pwd = $ pwd;
}

Function encode ($ str)
/*
Note: convert the KEY1 = VAL1 & KEY2 = VAL2 format to an array.
*/
{
$ Arr = explode ('&', $ str );
$ Return = array ();
Foreach ($ arr as $ k => $ v)
{
List ($ key, $ val) = explode ('=', $ v );
$ Return [$ key] = $ val;
$ This-> chs = NULL;
}
Return $ return;
}

Function utf8_to_gb2312 ($ str)
{
$ This-> chs = new Chinese ("UTF8", "GB2312", $ str );
Return $ this-> chs-> ConvertIT ();
}

Function gb2312_to_utf8 ($ str)
{
$ This-> chs = new Chinese ("GB2312", "UTF8", $ str );
Return $ this-> chs-> ConvertIT ();
}

Function query ($ str)
{
$ This-> httpclient = new http (HTTP_V11, true );
$ This-> httpclient-> host = 'kconn .tencent.com ';
$ This-> httpcilent-> port = 21001;

$ Query = $ this-> encode ($ str );
$ Status = $ this-> httpclient-> post ('', $ query ,'');
If ($ status = HTTP_STATUS_ OK ){
Return $ this-> httpclient-> get_response_body ();
}
Else
{
Print_r ($ this-> httpclient );
Return false;
}
$ This-> httpclient-> disconnect ();
Unset ($ this-> httpclient );
}

Function split_str ($ str)
{
$ Arr = explode (",", $ str );
If ($ arr [count ($ arr)-1] = NULL)
{
Unset ($ arr [count ($ arr)-1]);
}
Return $ arr;
}

Function login ()
{
// Login
// VER = 1.1 & CMD = Login & SEQ = & UIN = & PS = & M5 = 1 & LC = 9326B87B234E7235
$ Str = "VER = 1.1 & CMD = Login & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& PS = ". md5 ($ this-> pwd ). "& M5 = 1 & LC = 9326B87B234E7235 ";
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// Return success
If ($ return ['Rs '] = 0)
{
// Login successful
Return QQ_LOGIN_SUCCESS;
}
Else
{
// Login failed
$ GLOBALS ['qq _ ERROR_MSG '] = $ this-> utf8_to_gb2312 ($ return ['A']);
Return QQ_LOGIN_FAILED;
}
}
Else
{
// Failed to return
Return QQ_RETURN_FAILED;

}
}

Function getFriendsList ()
{
// Obtain the friend list
// VER = 1.1 & CMD = List & SEQ = & UIN = & TN = 160 & UN = 0
$ Str = "VER = 1.1 & CMD = List & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& TN = 160 & UN = 0 ";
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// Return success
Return $ this-> split_str ($ return ['un ']);
}
Else
{
// Failed to return
Return QQ_RETURN_FAILED;

}
}

Function getOnlineList ()
{
// Obtain the online friend list
// VER = 1.1 & CMD = Query_Stat & SEQ = & UIN = & TN = 50 & UN = 0
$ Str = "VER = 1.1 & CMD = Query_Stat & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& TN = 50 & UN = 0 ";
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// Return success
If ($ return ['SN ']> 0)
{
// Number of online friends> 0
$ Uns = $ this-> split_str ($ return ['un']); // Number List
$ Nks = $ this-> split_str ($ return ['NK ']); // nickname list
$ Sts = $ this-> split_str ($ return ['st']); // status list
$ Fcs = $ this-> split_str ($ return ['FC ']); // avatar list
$ Error = 0;
(Count ($ uns) = count ($ nks) = (count ($ sts) = count ($ fcs) = (count ($ nks) = count ($ sts ))?
$ Num = count ($ uns)
:
$ Error = 1;
;
If ($ error = 1) return QQ_LIST_ERROR;
$ Arr = array ();
For ($ I = 0; $ I <$ num; $ I ++)
{
$ Arr [] = array (
"UN" => $ uns [$ I],
"NK" => $ this-> utf8_to_gb2312 ($ nks [$ I]),
"ST" => $ sts [$ I],
"FC" => $ fcs [$ I]
);
}
Return ($ arr );
}
Else
{
// Number of Online Friends <= 0
Return QQ_LIST_NONE;
}

}
Else
{
// Failed to return
Return QQ_RETURN_FAILED;

}
}

Function getInfo ($ uin)
{
// Obtain friend information
// AD is the contact address, AG is the age, EM is the MAIL, FC is the avatar, HP is the website, JB is the occupation, PC is the zip code, PH is the contact phone number, PR is the introduction, PV is the province, RN is the real name, SC is the graduate school, SX is the gender, UN is the QQ number, NK is the QQ nickname
// The following comments study by Hackfan
// BT is the blood type, CO is the constellation, CT is the city, CY is the country, MO is the mobile phone, SH Zodiac
// LV indicates the query number (1 indicates a simplified query, 2 indicates a common query, and 3 indicates a detailed query)
// The CV is unknown, and the ID is unknown (ID card ?), Unknown MT, unknown MV,
// VER = 1.1 & CMD = GetInfo & SEQ = & UIN = & LV = 3 & UN =
$ Str = "VER = 1.1 & CMD = GetInfo & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& LV = 3 & UN = ". $ uin;
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// Return success
$ Arr = array
(
'Ad' => $ this-> utf8_to_gb2312 ($ return ['ad']), // contact address
'AG' => $ this-> utf8_to_gb2312 ($ return ['AG']), // age
'BT '=> $ return ['BT'], // blood type
'Co' => $ return ['Co'], // constellation
'CT '=> $ this-> utf8_to_gb2312 ($ return ['CT']), // City
'Cy '=> $ this-> utf8_to_gb2312 ($ return ['cy']), // country
'Em '=> $ this-> utf8_to_gb2312 ($ return ['em']), // Email
'FC '=> $ return ['FC'], // Avatar
'HP '=> $ this-> utf8_to_gb2312 ($ return ['HP']), // website
'JB '=> $ this-> utf8_to_gb2312 ($ return ['JB']), // occupation
'M' => $ return ['M'], // mobile phone number
'PC' => $ this-> utf8_to_gb2312 ($ return ['PC']), // ZIP code
'Ph '=> $ this-> utf8_to_gb2312 ($ return ['Ph']), // contact number
'PR' => $ this-> utf8_to_gb2312 ($ return ['PR']), // Introduction
'Pv' => $ this-> utf8_to_gb2312 ($ return ['pv']), // Save
'RN' => $ this-> utf8_to_gb2312 ($ return ['RN ']), // real name
'Scs' => $ this-> utf8_to_gb2312 ($ return ['scs']), // Graduate School
'Sh' => $ return ['sh'], // Zodiac
'Sx '=> $ return ['sx'], // gender
'Non' => $ return ['non'], // QQ number
'NK '=> $ this-> utf8_to_gb2312 ($ return ['NK']) // nickname
);
Return $ arr;
}
Else
{
// Failed to return
Return QQ_RETURN_FAILED;

}

}

Function addFriend ($ uin)
{
// Add new friends
// VER = 1.1 & CMD = AddToList & SEQ = & UIN = & UN =
$ Str = "VER = 1.1 & CMD = AddToList & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& UN = ". $ uin;
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// Return success
Switch ($ return ['CD'])
{
Case 0:
// The other party allows anyone to add as friends
Return QQ_ADDTOLIST_SUCCESS;
Break;
Case 1:
// Verification required
Return QQ_ADDTOLIST_NEEDAUTH;
Break;
Case 3:
// No one is allowed to add a friend
Return QQ_ADDTOLIST_REFUSE;
Break;
Default:
// Unknown code
Return QQ_ADDTOLIST_UNKNOWN;
Break;
}
}
Else
{
// Failed to return
Return QQ_RETURN_FAILED;
}
}

Function replyAdd ($ uin, $ type, $ msg)
{
// Respond to add friends
// VER = 1.1 & CMD = Ack_AddToList & SEQ = & UIN = & UN = & CD = & RS =
// When CD is in the response status and CD is 0, it indicates "verification successful ". If the value of CD is 1, it indicates that "the peer is rejected as a friend ". If CD is set to 2, "add a friend to the requester" is displayed ". RS is the reason for your request
$ Str = "VER = 1.2 & CMD = Ack_AddToList & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& UN = ". $ uin. "& CD = ". $ type. "& RS = ". $ this-> gb2312_to_utf8 ($ msg );
$ Return = $ this-> encode ($ this-> query ($ str ));

If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// The server receives the message successfully.
Return QQ_RETURN_SUCCESS;
}
Else
{
// Failed
Return QQ_RETURN_FAILED;
}
}

Function delFriend ($ uin)
{
// Delete a friend
// VER = 1.1 & CMD = DelFromList & SEQ = & UIN = & UN =
$ Str = "VER = 1.1 & CMD = DelFromList & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& UN = $ uin ";
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// The server receives the message successfully.
Return QQ_RETURN_SUCCESS;
}
Else
{
// Failed
Return QQ_RETURN_FAILED;
}
}

Function changeStatus ($ status)
{
// Change the status
// VER = 1.1 & CMD = Change_Stat & SEQ = & UIN = & ST =
// ST indicates the status to be changed, 10 indicates Online, 20 indicates offline, and 30 indicates busy.
$ Str = "VER = 1.1 & CMD = Change_stat & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& ST = ". $ status;
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// The server receives the message successfully.
Return QQ_RETURN_SUCCESS;
}
Else
{
// Failed
Return QQ_RETURN_FAILED;
}
}

Function logout ()
{
// Log out
// VER = 1.1 & CMD = Logout & SEQ = & UIN =
$ Str = "VER = 1.1 & CMD = Logout & SEQ =". rand (,). "& UIN =". $ this-> uin;
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// The server receives the message successfully.
Return QQ_RETURN_SUCCESS;
}
Else
{
// Failed
Return QQ_RETURN_FAILED;
}
}

Function getMsg ()
{
// Obtain the message
// VER = 1.1 & CMD = GetMsgEx & SEQ = & UIN =
// MT indicates the message type, 99 indicates the system message, and 9 indicates the user message. "UN" indicates the user who sent the message, "MG" indicates the message sent, and "MG" indicates some specific system meanings.
// When MT = 99: MG = 10 indicates that the user is online, MG = 20 indicates that the user is offline, and MG = 30 indicates that the user is busy.
$ Str = "VER = 1.1 & CMD = GetMsgEx & SEQ =". rand (,). "& UIN =". $ this-> uin;
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// The server receives the message successfully.
If ($ return ['mn ']> 0)
{
// Number of messages> 0
$ Mts = $ this-> split_str ($ return ['mt']); // message type
$ Uns = $ this-> split_str ($ return ['un']); // The sender's number.
$ Mgs = $ this-> split_str ($ return ['MG ']); // message content
$ Error = 0;
(Count ($ mts) = count ($ uns) = (count ($ uns) = count ($ mgs ))?
$ Num = count ($ uns)
:
$ Error = 1;
;
If ($ error = 1) return QQ_GETMSG_ERROR; // incorrect business trip
$ Arr = array ();
For ($ I = 0; $ I <$ num; $ I ++)
{
$ Arr [] = array (
"MT" => $ mts [$ I],
"UN" => $ uns [$ I],
"MG" => $ this-> utf8_to_gb2312 ($ mgs [$ I])
);
}
Return ($ arr );
}
Else
{
// Number of Online Friends <= 0
Return QQ_GETMSG_NONE;
}
}
Else
{
// Failed
Return QQ_RETURN_FAILED;
}
}

Function sendMsg ($ uin, $ msg)
{
// Send a message
// VER = 1.1 & CMD = CLTMSG & SEQ = & UIN = & UN = & MG =
$ Str = "VER = 1.1 & CMD = CLTMSG & SEQ = ". rand (1000,9000 ). "& UIN = ". $ this-> uin. "& UN = ". $ uin. "& MG = ". $ this-> gb2312_to_utf8 ($ msg );
$ Return = $ this-> encode ($ this-> query ($ str ));
If ($ return ['res'] = 0 and $ return ['uin'] = $ this-> UIN)
{
// The server receives the message successfully.
Return QQ_RETURN_SUCCESS;
}
Else
{
// Failed
Return QQ_RETURN_FAILED;
}
}

}
?>

Related Article

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.