Taobaoip.class.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Lib;
  15. class Taobaoip {
  16. /**
  17. * 淘宝IP地址库 Reset API
  18. * @author Chunice <hrb@usa.com>
  19. * @param [string] $ip [IP地址]
  20. * @return [type] [只返回获取成功的ip数据]
  21. */
  22. public function getLocation($ip) {
  23. if (empty($ip)) $ip = get_client_ip();
  24. $taobaoUrl = "http://ip.taobao.com/service/getIpInfo.php?ip=";
  25. $url = $taobaoUrl . $ip;
  26. $data = self::httpRequest($url);
  27. $data = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $data);
  28. $data = json_decode($data, true);
  29. return $data[data];
  30. }
  31. Static Private function httpRequest($url) {
  32. $ch = curl_init();
  33. curl_setopt($ch, CURLOPT_URL, $url);
  34. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  35. $output = curl_exec($ch);
  36. curl_close($ch);
  37. if ($output === FALSE) {
  38. return "cURL Error: " . curl_error($ch);
  39. }
  40. return $output;
  41. }
  42. }
  43. ?>