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 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: |
Copy code |
<? Php 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: |
Copy code |
<? 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
The Code is as follows: |
Copy code |
// 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; } |