jfcherng / php-ip-location

通过IP查询地理位置。

3.0.6 2023-03-07 08:22 UTC

This package is auto-updated.

Last update: 2024-09-07 13:35:34 UTC


README

GitHub Workflow Status (branch) Packagist Packagist Version Project license GitHub stars Donate to this project using Paypal

使用 IPIPcz88 (純真) 两个数据库来查询IP的地理位置。

安装流程

  1. 使用 Composer 安装: composer require jfcherng/php-ip-location

  2. 这样就完成了,但如果你想要自己更新IP数据库,请参考以下步骤:

    1. 获取 IPIP.net 的 IP 离线数据库 (ipipfree.ipdb)

    2. 純真 IP 数据库 (qqwry.dat) 的 IPDB 格式版本

    3. 在使用时自行设置两个数据库的路径

使用方法

demo.php

<?php

use Jfcherng\IpLocation\IpLocation;

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

$ipFinder = IpLocation::getInstance();

// 如果不想要使用內建的 IP 資料庫,請進行以下設定
$ipFinder->setup([
    // ipip 資料庫的路徑
    'ipipDb' => __DIR__ . '/src/db/ipipfree.ipdb',
    // cz88 資料庫的路徑
    'cz88Db' => __DIR__ . '/src/db/qqwry.ipdb',
]);

$ip = '202.113.245.255';

$results = $ipFinder->find($ip);

\var_dump($results);
/*
array(5) {
  ["country_name"]=>
  string(6) "中国"
  ["region_name"]=>
  string(6) "天津"
  ["city_name"]=>
  string(6) "天津"
  ["owner_domain"]=>
  string(0) ""
  ["isp_domain"]=>
  string(9) "教育网"
}
*/