Php obtains qq user nickname and online status (instance analysis)

Source: Internet
Author: User

QQ returns different images to indicate online or offline, and the icons also change. Since the images are different, the Content-Length in the returned HTTP header must be different, A color image must be larger than a dark image in the same way. Therefore, you can find the middle value of a color image or a dark image, you can obtain the online QQ status by determining the length of the returned header.
The following code is used:
Copy codeThe Code is as follows:
<? Phpfunction get_qq_status ($ uin)
{
Error_reporting (0 );
$ F = file_get_contents ('HTTP: // wpa.qq.com/pa? P = 1: '. $ uin.': 4 ');
If (! $ F) return (true );
Foreach ($ http_response_header as $ val)
{
If (strpos ($ val, 'content-length ')! = False)
{
Return (intval (substr ($ val, 1000)> );
}
}
}

?>

The above is relatively simple. Here is a better
Copy codeThe Code is as follows:
<?
Function tphp_qq_online ($ uin)
{
$ Reques = "GET/pa? P = 1: ". $ uin.": 1 HTTP/1.1rn ";
$ Reques. = "Host: wpa. qq. comrn ";
$ Reques. = "User-Agent: PHP_QQ_SPYrnrn ";

If (! ($ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) return (-1 );
If (! (Socket_connect ($ socket, "wpa.qq.com", 80) return (-1 );
If (! (Socket_write ($ socket, $ reques) return (-1 );
If (! ($ Respon = socket_read ($ socket, 1024, PHP_BINARY_READ) return (-1 );;
Socket_close ($ socket );

$ Field = explode ("rn", $ respon );
For ($ I = 0; $ I <count ($ field); $ I ++ ){
If (strncasecmp ($ field [$ I], "Location:", 9) = 0 ){
If (strpos ($ field [$ I], "online ")){
$ Ret = 1;
} Else if (strpos ($ field [$ I], "offline ")){
$ Ret = 0;
} Else {
$ Ret =-1;
} // If
Break;
} // If
} //

Return ($ ret );
}
/*}}}*/

 

Echo tphp_qq_online (561272831 );

?>

For example, qq user nickname and online status
Copy codeThe Code is as follows:
// Obtain QQ status
Function getQQState ($ qq ){
$ Url = 'HTTP: // wpa.qq.com/pa? P = 2: '. $ qq.': 41 & r = '. time ();
$ HeadInfo = get_headers ($ url, 1 );
$ Length = $ headInfo ['content-length'];
If ($ length = 1243 ){
Return true;
} Else {
Return false;
}
}
// Obtain QQ nickname
Function getQQNick ($ qq ){
$ Str = file_get_contents ('HTTP: // r.qzone.qq.com/cgi-bin/user/cgi_personal_card? Uin = '. $ qq );
$ Pattern = '/'. preg_quote ('"nickname ":"','/').'(.*?) '. Preg_quote (' ", ','/').'/I ';
Preg_match ($ pattern, $ str, $ result );
Return $ result [1];
}
// Obtain QQ name
Function getQQName ($ qq ){
// $ QqArr = include 'friendarr. php'; // pre-configured
// $ Username = $ qqArr [$ qq];
If (! $ Username ){
$ Username = getQQNick ($ qq );
}
Return $ username;
}

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.