Use the Sina IP library to obtain the detailed IP address

Source: Internet
Author: User
Tags get ip
Use the Sina IP library to obtain the detailed IP address
Use the Sina IP library to obtain the detailed IP address

  1. Class Tool {
  2. /**
  3. * Obtain the IP address location (Sina IP library)
  4. *
  5. * @ Param $ ip String ip address: 112.65.102.16
  6. * @ Return Array
  7. */
  8. Static public function getIpCity ($ ip)
  9. {
  10. $ Ip = preg_replace ("/\ s/", "", preg_replace ("/\ r \ n/", "", $ ip ));
  11. $ Link = "http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = ". $ ip." & t = ". time ();
  12. $ IpJson = self: httpCurl ($ link );
  13. Preg_match ("/\" country \ ": \" (. *) \ "/Uis", $ ipJson, $ mattings );
  14. Preg_match ("/\" province \ ": \" (. *) \ "/Uis", $ ipJson, $ match2 );
  15. Preg_match ("/\" city \ ": \" (. *) \ "/Uis", $ ipJson, $ match3 );
  16. Return array (
  17. 'Country' => self: ucode2zh ($ mattings [1]), // country
  18. 'Province '=> self: ucode2zh ($ match2 [1]), // province
  19. 'City' => self: ucode2zh ($ match3 [1]) // city
  20. );
  21. }
  22. /**
  23. * Curl information retrieval
  24. */
  25. Static public function httpCurl ($ url)
  26. {
  27. $ Curl_handle = curl_init ();
  28. Curl_setopt ($ curl_handle, CURLOPT_URL, $ url );
  29. Curl_setopt ($ curl_handle, CURLOPT_CONNECTTIMEOUT, 2 );
  30. Curl_setopt ($ curl_handle, CURLOPT_RETURNTRANSFER, 1 );
  31. Curl_setopt ($ curl_handle, CURLOPT_FAILONERROR, 1 );
  32. $ File_content = curl_exec ($ curl_handle );
  33. Curl_close ($ curl_handle );
  34. Return $ file_content;
  35. }
  36. /**
  37. * Convert unicode to Chinese. if conversion fails, the original string is returned.
  38. *
  39. * @ Param $ code String unicode encoding
  40. * @ Return String
  41. */
  42. Static public function ucode2zh ($ code)
  43. {
  44. $ Temp = explode ('\ U', $ code );
  45. $ Rslt = array ();
  46. Array_shift ($ temp );
  47. Foreach ($ temp as $ k => $ v)
  48. {
  49. $ V = hexdec ($ v );
  50. $ Rslt [] = '& #'. $ v .';';
  51. }
  52. $ R = implode ('', $ rslt );
  53. Return empty ($ r )? $ Code: $ r;
  54. }
  55. }

Get IP address class use instance

  1. $ IpStr = Tool: getIpCity ('192. 65.102.16 ');
  2. Print_r ($ ipStr );
  3. # Returned results
  4. Array ([country] => China [province] => Shanghai [city] => Shanghai)

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.