IP.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Components;
  3. use Exception;
  4. use GeoIp2\Database\Reader;
  5. use GeoIp2\Exception\AddressNotFoundException;
  6. use Http;
  7. use IP2Location\Database;
  8. use Ip2Region;
  9. use ipip\db\City;
  10. use Log;
  11. use MaxMind\Db\Reader\InvalidDatabaseException;
  12. class IP
  13. {
  14. // 获取IP地址信息
  15. public static function getIPInfo($ip)
  16. {
  17. // IPv6 推荐使用ip.sb
  18. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  19. Log::info('识别到IPv6,尝试解析:'.$ip);
  20. $ipInfo = self::IPSB($ip);
  21. } else {
  22. $ipInfo = self::ip2Region($ip);
  23. if (!$ipInfo) {
  24. Log::info('无法识别,尝试使用【IPIP库】库解析:'.$ip);
  25. $ipInfo = self::ip2Location($ip);
  26. }
  27. }
  28. return $ipInfo;
  29. }
  30. // 通过api.ip.sb查询IP地址的详细信息
  31. public static function IPSB($ip)
  32. {
  33. $response = Http::timeout(15)->get('https://api.ip.sb/geoip/'.$ip);
  34. if ($response->ok()) {
  35. return $response->json();
  36. }
  37. Log::error('解析IPv6异常:'.$ip);
  38. return false;
  39. }
  40. // 通过ip2Region查询IP地址的详细信息 ← 聚合 淘宝IP库,GeoIP,纯真IP库
  41. public static function ip2Region(string $ip)
  42. {
  43. $ipInfo = false;
  44. try {
  45. $ipInfo = (new Ip2Region())->memorySearch($ip);
  46. } catch (Exception $e) {
  47. Log::error('【淘宝IP库】错误信息:'.$e->getMessage());
  48. }
  49. if ($ipInfo) {
  50. $location = explode("|", $ipInfo['region']);
  51. return [
  52. 'country' => $location[0] ?: '',
  53. 'province' => $location[2] ?: '',
  54. 'city' => $location[3] ?: '',
  55. ];
  56. }
  57. return $ipInfo;
  58. }
  59. //// 通过ip2Location查询IP地址的详细信息
  60. public static function ip2Location(string $ip)
  61. {
  62. $filePath = database_path('IP2LOCATION-LITE-DB3.IPV6.BIN');
  63. try {
  64. $location = (new Database($filePath, Database::FILE_IO))
  65. ->lookup($ip, [Database::CITY_NAME, Database::REGION_NAME, Database::COUNTRY_NAME,]);
  66. return [
  67. 'country' => $location['countryName'],
  68. 'province' => $location['regionName'],
  69. 'city' => $location['cityName'],
  70. ];
  71. } catch (Exception $e) {
  72. Log::error('【ip2Location】错误信息:'.$e->getMessage());
  73. }
  74. return false;
  75. }
  76. // 通过IPIP查询IP地址的详细信息
  77. public static function IPIP(string $ip): array
  78. {
  79. $filePath = database_path('ipip.ipdb');
  80. $location = (new City($filePath))->findMap($ip, 'CN');
  81. return [
  82. 'country' => $location['country_name'],
  83. 'province' => $location['region_name'],
  84. 'city' => $location['city_name'],
  85. ];
  86. }
  87. // 通过ip.taobao.com查询IP地址的详细信息
  88. public static function TaoBao(string $ip)
  89. {
  90. // 依据 http://ip.taobao.com/instructions 开发
  91. $response = Http::timeout(15)->get('http://ip.taobao.com/outGetIpInfo?ip='.$ip.'&accessKey=alibaba-inc');
  92. if ($response->ok()) {
  93. $message = $response->json();
  94. if ($message['code'] === 0) {
  95. return [
  96. 'country' => $message['data']['country'] === "XX" ? '' : $message['data']['country'],
  97. 'province' => $message['data']['region'] === "XX" ? '' : $message['data']['region'],
  98. 'city' => $message['data']['city'] === "XX" ? '' : $message['data']['city'],
  99. ];
  100. }
  101. Log::error('【淘宝IP库】返回错误信息:'.$ip.PHP_EOL.$message['msg']);
  102. } else {
  103. Log::error('【淘宝IP库】解析异常:'.$ip);
  104. }
  105. return false;
  106. }
  107. // 通过api.map.baidu.com查询IP地址的详细信息
  108. public static function Baidu(string $ip)
  109. {
  110. if (!env('BAIDU_APP_AK')) {
  111. Log::error('【百度IP库】AK信息缺失');
  112. return false;
  113. }
  114. // 依据 http://lbsyun.baidu.com/index.php?title=webapi/ip-api 开发
  115. $response = Http::timeout(15)->get('https://api.map.baidu.com/location/ip?ak='.env('BAIDU_APP_AK').'&'.$ip.'&coor=bd09ll');
  116. if ($response->ok()) {
  117. $message = $response->json();
  118. if ($message['status'] === 0) {
  119. return [
  120. 'country' => $message['content']['address_detail']['country'],
  121. 'province' => $message['content']['address_detail']['province'],
  122. 'city' => $message['content']['address_detail']['city'],
  123. ];
  124. }
  125. Log::error('【百度IP库】返回错误信息:'.$ip.PHP_EOL.var_export($message['message'], true));
  126. } else {
  127. Log::error('【百度IP库】解析异常:'.$ip);
  128. }
  129. return false;
  130. }
  131. // 通过GeoIP2查询IP地址的详细信息
  132. public static function GeoIP2(string $ip)
  133. {
  134. $filePath = database_path('maxmind.mmdb');
  135. try {
  136. $location = (new Reader($filePath))->city($ip);
  137. return [
  138. 'country' => $location->country->names['zh-CN'],
  139. 'province' => '',
  140. 'city' => $location->city->name ?? '',
  141. ];
  142. } catch (AddressNotFoundException $e) {
  143. Log::error('【GeoIP2】查询失败:'.$ip);
  144. } catch (InvalidDatabaseException $e) {
  145. Log::error('【GeoIP2】数据库无效:'.$ip);
  146. }
  147. return false;
  148. }
  149. // 获取访客真实IP
  150. public static function getClientIP()
  151. {
  152. /*
  153. * 访问时用localhost访问的,读出来的是“::1”是正常情况
  154. * ::1说明开启了IPv6支持,这是IPv6下的本地回环地址的表示
  155. * 使用IPv4地址访问或者关闭IPv6支持都可以不显示这个
  156. */
  157. if (isset($_SERVER)) {
  158. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  159. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  160. $ip = $_SERVER['REMOTE_ADDR'];
  161. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  162. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  163. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  164. $ip = $_SERVER['HTTP_CLIENT_IP'];
  165. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  166. $ip = $_SERVER['REMOTE_ADDR'];
  167. } else {
  168. $ip = 'unknown';
  169. }
  170. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  171. $ip = getenv('HTTP_X_FORWARDED_FOR');
  172. } elseif (getenv('HTTP_CLIENT_IP')) {
  173. $ip = getenv('HTTP_CLIENT_IP');
  174. } else {
  175. $ip = getenv('REMOTE_ADDR');
  176. }
  177. if (trim($ip) === '::1') {
  178. $ip = '127.0.0.1';
  179. }
  180. return $ip;
  181. }
  182. }