atchondjo/geoip2country

一个轻量但强大的IP地址查询数据库解决方案,用于确定访客所在国家

v2.0.6 2023-04-16 01:11 UTC

This package is auto-updated.

Last update: 2024-09-14 11:24:36 UTC


README

Donate Build Status Scrutinizer Code Quality Code Intelligence Status GitHub license

IP地址只是无尽可能性、冒险和发展的起点...

GeoIP2Country PRO是什么?

一个PHP IP地址地理位置库,帮助您识别访客的地理位置。这个组件注重保持其轻量级和快速查找。运行时没有外部请求。因此,如果您需要获取网站访客的国家,并且不想使用任何API,那么这是您的最佳解决方案。该项目包括方法,以尽可能少的人工干预更新您的GEOIP数据库(仅限捐赠者)。

本版本的新功能有哪些?

  • 处理算法的完全重构和优化
  • 一个新的数据库引擎(结合安全性和效率)
  • 一个新的用于更新数据库的组件?
  • 基于ICANN(互联网名称与数字地址分配机构)统计数据的存储库

注意

为了鼓励人们支持此项目,数据库更新组件不包括在此存储库中,因为它们仅限于捐赠者。因此,任何捐赠者都将收到此软件的完整副本,包括帮助他使GEOIP数据库保持更新的组件。然而,其他用户仍然可以使用此处发布的软件。只有数据库更新功能将不可用

在此捐赠: Donate

要求

  • pdo_sqlite(运行时依赖)
  • php_curl(仅用于更新)

安装和初始化

以下说明将在您的本地机器上运行项目副本。有两种选择

  • 将GeoIP2Country文件导入(复制)到项目中的特定文件夹
  • 使用Composer安装程序(推荐)通过键入以下命令
composer require atchondjo/geoip2country

用法:(使用Composer自动加载器)

require __DIR__ . '/vendor/autoload.php';

try
{
    $IP2Country = new iriven\GeoIP2Country();

} catch (\Throwable $th) {
    trigger_error($th->getMessage(), E_USER_ERROR);
}

从给定的IP地址获取国家代码

    $ipAddress_1='2a00:1450:4007:816::2004';
    $ipAddress_2='37.140.250.97';
    $ipAddress_3='2001:41d0:301::21';
    $ipAddress_4='216.58.201.228';
    $ipAddress_5='188.165.53.185';
    $ipAddress_6='10.10.201.12';

    echo '<pre>';
    echo $IP2Country->resolve($ipAddress_1).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_2).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_3).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_4).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_5).PHP_EOL;
    echo $IP2Country->resolve($ipAddress_6).PHP_EOL;

获取当前访客国家代码(自动检测其IP地址)

    echo '<pre>';
    echo $IP2Country->resolve().PHP_EOL;  /** resolve() method called without any argument */

检索国家名称

因为我的其中一篇出版物已经涉及从ISO代码恢复国家名称。因此,在这个项目中,我选择仅限于从给定的IP地址搜索国家的ALPHA2 ISO代码。要检索国家名称(以及更多),您必须实例化从此处可用的"WorldCountriesDatas"类,并将前一个命令的结果传递如下

$CountryName ='n/a';
$CountryCode = $IP2Country->resolve($ipAddress);
if(!$IP2Country->isReservedAddress($ipAddress))
{
  require_once 'WorldCountriesDatas.php';
  $DataProvider = new \Iriven\WorldCountriesDatas();
  $CountryName = $DataProvider->getCountryName($CountryCode);
}

更新GeoIP数据:(仅限捐赠者)

require __DIR__ . '/vendor/autoload.php';

try
{
    $IP2CountryBackend = new iriven\GeoIP2CountryServer();
    $IP2CountryBackend->updateDatabase();

} catch (\Throwable $th) {
    trigger_error($th->getMessage(), E_USER_ERROR);
}

/*
* NOTE: In order to encourage people to support this project, database update components are not included in this
* repository as they are for donors only. Thus any donor will receive a full copy of this software,
* including the component that will help him to make his GEOIP database stay up to date.
*/

兼容性

  • IPV4
  • IPV6

作者

许可协议

本项目遵循GNU通用公共许可证V3版本 - 详细内容请参阅LICENSE文件

捐赠

如果这个项目帮助您减少了开发时间,您可以给我一杯咖啡:)

Donate

致谢

  • 本项目使用ICANN的GeoIp数据,可从此处获取

免责声明

如果您在您的项目中使用此库,请通过以下代码在此页面上添加回链。

<a href="https://github.com/iriven/GeoIP2Country" target="_blank">This Project Uses Alfred's TCHONDJO GeoIP2Country PRO.</a>