jfcherng/php-ip-location

利用 IP 查詢地理位置。

3.0.6 2023-03-07 08:22 UTC

This package is auto-updated.

Last update: 2024-04-07 12:28: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) "教育网"
}
*/