Php obtains qq user nicknames and online status instances. If we use php to obtain the QQ user name and online status QQ does not provide us with an api interface, if we want to obtain it, we can use QQ space or QQ Web edition chat. QQ returns different results. if we use php to obtain the QQ user name and online status, QQ does not provide us with an api. if we want to obtain the user name, we can use QQ space or QQ web chat.
QQ returns different images to indicate online or offline, and the icons change accordingly.
Since the images are different, the Content-Length in the returned HTTP header must be different, and the color image must be larger than the dark one. therefore, find the middle value of a color and dark color image of a style to obtain the online QQ status by determining the length of the returned header.
The following code is used:
The code is as follows: |
|
Function 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
The 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 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
The 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; } |
Bytes. QQ returns different results...