Php obtains qq user nicknames and online status instances

Source: Internet
Author: User
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:

  1. Function get_qq_status ($ uin)
  2. {
  3. Error_reporting (0 );
  4. $ F = file_get_contents ('http: // wpa.qq.com/pa? P = 1: '. $ uin.': 4 ');
  5. If (! $ F) return (true );
  6. Foreach ($ http_response_header as $ val)
  7. {
  8. If (strpos ($ val, 'Content-length ')! = False)
  9. {
  10. Return (intval (substr ($ val, 1000)> );
  11. }
  12. }
  13. }
  14. ?>

The above is relatively simple. the code below is better:

  1. Function tphp_qq_online ($ uin)
  2. {
  3. $ Reques = "GET/pa? P = 1: ". $ uin.": 1 HTTP/1.1rn ";
  4. $ Reques. = "Host: wpa. qq. comrn ";
  5. $ Reques. = "User-Agent: PHP_QQ_SPYrnrn ";
  6. If (! ($ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) return (-1 );
  7. If (! (Socket_connect ($ socket, "wpa.qq.com", 80) return (-1 );
  8. If (! (Socket_write ($ socket, $ reques) return (-1 );
  9. If (! ($ Respon = socket_read ($ socket, 1024, PHP_BINARY_READ) return (-1 );;
  10. Socket_close ($ socket );
  11. $ Field = explode ("rn", $ respon );
  12. For ($ I = 0; $ I
  13. If (strncasecmp ($ field [$ I], "Location:", 9) = 0 ){
  14. If (strpos ($ field [$ I], "online ")){
  15. $ Ret = 1;
  16. } Else if (strpos ($ field [$ I], "offline ")){
  17. $ Ret = 0;
  18. } Else {
  19. $ Ret =-1;
  20. } // If
  21. Break;
  22. } // If
  23. } //
  24. Return ($ ret );
  25. }
  26. /*}}}*/
  27. Echo tphp_qq_online (561272831 );
  28. ?>

For example, qq user nickname and online status. the code is as follows:

  1. // Obtain QQ status
  2. Function getQQState ($ qq ){
  3. $ Url = 'http: // wpa.qq.com/pa? P = 2: '. $ qq.': 41 & r = '. time ();
  4. $ HeadInfo = get_headers ($ url, 1 );
  5. $ Length = $ headInfo ['content-length'];
  6. If ($ length = 1243 ){
  7. Return true;
  8. } Else {
  9. Return false;
  10. }
  11. }
  12. // Obtain QQ nickname
  13. Function getQQNick ($ qq ){
  14. $ Str = file_get_contents ('http: // r.qzone.qq.com/cgi-bin/user/cgi_personal_card? Uin = '. $ qq );
  15. $ Pattern = '/'. preg_quote ('"nickname ":"','/').'(.*?) '. Preg_quote (' ", ','/').'/I ';
  16. Preg_match ($ pattern, $ str, $ result );
  17. Return $ result [1];
  18. }
  19. // Obtain QQ name
  20. Function getQQName ($ qq ){
  21. // $ QqArr = include 'friendarr. php'; // pre-configured
  22. // $ Username = $ qqArr [$ qq];
  23. If (! $ Username ){
  24. $ Username = getQQNick ($ qq );
  25. }
  26. Return $ username;
  27. }

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.