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
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 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 QQ online status by determining the length of the returned header. the following code is used:
-
- 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. the code below is better:
-
- 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;
- }