PHP code example of zoujingli / ip2region

1. Go to this page and download the library: Download zoujingli/ip2region library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zoujingli / ip2region example snippets


echo ip2region('61.142.118.231'); 
// 输出:中国广东省中山市【电信】

echo ip2region('114.114.114.114'); 
// 输出:中国江苏省南京市


{
    // 使用自定义数据库路径(建议使用绝对路径)
    $ip2region = new \Ip2Region('file', 
        '/path/to/your/ip2region_v4.xdb',  // IPv4 数据库路径
        '/path/to/your/ip2region_v6.xdb'   // IPv6 数据库路径
    );
    
    // 查询示例
    echo $ip2region->simple('61.142.118.231'); // 中国广东省中山市【电信】
    echo $ip2region->simple('2400:3200::1'); // 阿里云 IPv6 DNS


��简单的使用方式
echo ip2region('61.142.118.231') . "\n"; // 中国广东省中山市【电信】(使用内置数据库)
echo ip2region('2400:3200::1') . "\n"; // 中国浙江省杭州市【专线用户】(需要下载完整数据库)

// 使用不同查询方法
echo ip2region('61.142.118.231', 'search'); // 中国|广东省|中山市|电信|CN
echo ip2region('61.142.118.231', 'memory'); // 返回数组格式

// 或者使用类方式
$ip2region = new \Ip2Region();
echo $ip2region->simple('61.142.118.231'); // 中国广东省中山市【电信】


��单查询
echo ip2region('61.142.118.231') . "\n";        // 中国广东省中山市【电信】
echo ip2region('2400:3200::1') . "\n"; // 中国浙江省杭州市【专线用户】

// 使用不同查询方法
echo ip2region('61.142.118.231', 'search') . "\n"; // 中国|广东省|中山市|电信|CN
echo ip2region('61.142.118.231', 'memory') . "\n"; // 返回数组格式

// 批量查询
$ips = ['61.142.118.231', '114.114.114.114', '2400:3200::1'];
foreach ($ips as $ip) {
    echo "$ip => " . ip2region($ip) . "\n";
}


{
    // 默认模式(使用内置数据库)
    $ip2region = new \Ip2Region();
    
    // 如需使用自定义数据库,请参考下面的"自定义数据库配置"部分
    // $ip2region = new \Ip2Region('file', '/path/to/your/ip2region_v4.xdb', '/path/to/your/ip2region_v6.xdb');

    // 基础查询
    echo $ip2region->simple('61.142.118.231') . "\n";
    echo $ip2region->search('2400:3200::1') . "\n";

    // 获取详细信息
    $info = $ip2region->getIpInfo('61.142.118.231');
    print_r($info);
    // 输出: Array(
    //   [country] => 中国
    //   [province] => 广东省
    //   [city] => 中山市
    //   [isp] => 电信
    //   [ip] => 61.142.118.231
    //   [version] => v4
    //   [region] =>  // 已弃用,原用于世界级区域,现已不再使用
    // )

    // 批量查询
    $results = $ip2region->batchSearch(['61.142.118.231', '114.114.114.114']);
    print_r($results);

    // 性能监控
    $stats = $ip2region->getStats();
    echo "内存使用: " . $stats['memory_usage'] . " bytes\n";
    echo "IPv4 已加载: " . ($stats['v4_loaded'] ? '是' : '否') . "\n";
    echo "IPv6 已加载: " . ($stats['v6_loaded'] ? '是' : '否') . "\n";

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}


{
    // 使用自定义数据库路径(建议使用绝对路径)
    $ip2region = new \Ip2Region('file', '/path/to/your/ip2region_v4.xdb', '/path/to/your/ip2region_v6.xdb');

    // 查询IP
    echo $ip2region->simple('61.142.118.231') . "\n";

    // 检查是否使用自定义数据库
    $customStatus = $ip2region->isUsingCustomDb();
    echo "IPv4 使用自定义数据库: " . ($customStatus['v4'] ? '是' : '否') . "\n";
    echo "IPv6 使用自定义数据库: " . ($customStatus['v6'] ? '是' : '否') . "\n";

    // 动态设置数据库路径
    $ip2region->setCustomDbPaths('/path/to/v4.xdb', '/path/to/v6.xdb');

    // 获取数据库配置信息
    $dbInfo = $ip2region->getDatabaseInfo();
    echo "IPv4 路径: " . ($dbInfo['custom_v4_path'] ?: '默认内置') . "\n";
    echo "IPv6 路径: " . ($dbInfo['custom_v6_path'] ?: '下载目录/默认路径/需下载') . "\n";

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

// PHAR 环境中的使用方式
$searcher = new Ip2Region(); // 默认文件缓存模式

// 或使用其他缓存策略
$searcher = new Ip2Region('vectorIndex'); // 向量索引模式
$searcher = new Ip2Region('content');     // 内容缓存模式

// IPv4 查询(使用内置数据库)
echo $searcher->simple('61.142.118.231'); // 中国广东省中山市【电信】

// IPv6 查询(需要预先将数据库文件放入 PHAR)
echo $searcher->simple('2400:3200::1'); // 中国浙江省杭州市【专线用户】

    // 简单查询(默认)
    echo ip2region('61.142.118.231');
    // 输出: 中国广东省中山市【电信】

    // 详细查询
    echo ip2region('61.142.118.231', 'search');
    // 输出: 中国|广东省|中山市|电信|CN

    // 内存查询(返回数组)
    $result = ip2region('61.142.118.231', 'memory');
    // 输出: Array([city_id] => 0, [region] => 中国|广东省|中山市|电信|CN)

    // IPv6 查询
    echo ip2region('2400:3200::1');
    // 输出: 中国浙江省杭州市【专线用户】

    // 异常处理
    try {
        $result = ip2region('invalid-ip');
    } catch (Exception $e) {
        echo "错误: " . $e->getMessage();
        // 如果是 IPv6 查询失败(通常是项目缺少 IPv6 数据库文件),提示下载数据库
        if (strpos($e->getMessage(), 'IPv6') !== false) {
            echo "\n提示: IPv6 查询需要下载完整数据库,请运行: ./vendor/bin/ip2down download v6";
        }
    }
    

new Ip2Region($cachePolicy = 'file', $dbPathV4 = null, $dbPathV6 = null)

    // 默认文件缓存模式
    $ip2region = new Ip2Region();
    
    // 向量索引缓存模式(减少 IO 操作)
    $ip2region = new Ip2Region('vectorIndex');
    
    // 内容缓存模式(零 IO,但占用更多内存)
    $ip2region = new Ip2Region('content');
    
    // 使用自定义数据库路径
    $ip2region = new Ip2Region('file', '/path/to/your/ip2region_v4.xdb', '/path/to/your/ip2region_v6.xdb');
    
    // 自定义缓存策略 + 自定义数据库路径
    $ip2region = new Ip2Region('vectorIndex', '/path/to/v4.xdb', '/path/to/v6.xdb');
    

    $info = $ip2region->getIpInfo('61.142.118.231');
    // 返回: [
    //   'country' => '中国',
    //   'province' => '广东省',
    //   'city' => '中山市',
    //   'isp' => '电信',
    //   'ip' => '61.142.118.231',
    //   'version' => 'v4',
    //   'region' => '' // 已弃用
    // ]
    


{
    // 默认模式(使用内置数据库)
    $ip2region = new \Ip2Region();
    
    // 或者使用自定义数据库(建议使用绝对路径)
    // $ip2region = new \Ip2Region('file', '/path/to/your/ip2region_v4.xdb', '/path/to/your/ip2region_v6.xdb');

    // 查询前状态
    $statsBefore = $ip2region->getStats();
    echo "查询前内存使用: " . $statsBefore['memory_usage'] . " bytes\n";

    // 执行查询
    $result = $ip2region->simple('61.142.118.231');
    echo "查询结果: " . $result . "\n";

    // 查询后状态
    $statsAfter = $ip2region->getStats();
    echo "查询后内存使用: " . $statsAfter['memory_usage'] . " bytes\n";
    echo "IPv4 已加载: " . ($statsAfter['v4_loaded'] ? '是' : '否') . "\n";
    echo "IPv6 已加载: " . ($statsAfter['v6_loaded'] ? '是' : '否') . "\n";
    echo "IPv4 IO 次数: " . $statsAfter['v4_io_count'] . "\n";
    echo "IPv6 IO 次数: " . $statsAfter['v6_io_count'] . "\n";

    // 内存使用详情
    $memory = $ip2region->getMemoryUsage();
    echo "当前内存: " . $memory['current'] . "\n";
    echo "峰值内存: " . $memory['peak'] . "\n";

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}


��取缓存统计信息
$dbInfo = $searcher->getDatabaseInfo();
echo "IPv4已加载: " . ($dbInfo['v4_loaded'] ? '是' : '否') . "\n";
echo "IPv6已加载: " . ($dbInfo['v6_loaded'] ? '是' : '否') . "\n";
echo "缓存策略: " . $dbInfo['cache_policy'] . "\n";

// 获取性能统计
$stats = $searcher->getStats();
echo "内存使用: " . round($stats['memory_usage'] / 1024 / 1024, 2) . " MB\n";
echo "缓存策略: " . $stats['cache_policy'] . "\n";


// 文件缓存模式(默认)
$searcher = new Ip2Region('file');

// 向量索引模式
$searcher = new Ip2Region('vectorIndex');

// 内容缓存模式
$searcher = new Ip2Region('content');

// 查询 IP
echo $searcher->simple('61.142.118.231');

ip2region(string $ip, string $method = 'simple'): string|array|null


��单查询(默认方法)
echo ip2region('61.142.118.231'); // 输出: 中国广东省中山市【电信】

// 详细查询
echo ip2region('61.142.118.231', 'search'); // 输出: 中国|广东省|中山市|电信|CN

// 内存查询(返回数组)
$result = ip2region('61.142.118.231', 'memory');
print_r($result); // 输出: Array([city_id] => 0, [region] => 中国|广东省|中山市|电信|CN)

// IPv6查询
echo ip2region('2400:3200::1'); // 输出: 中国浙江省杭州市【专线用户】

// 异常安全
$result = ip2region('invalid-ip'); // 返回: null
if ($result === null) {
    echo "IP地址无效或查询失败";
}

Ip2Region (全局类)
├── 使用 ip2region\xdb\IPv4
├── 使用 ip2region\xdb\IPv6  
└── 使用 ip2region\xdb\Searcher

ip2region\xdb\* (组件类)
├── Util.php       # 工具类
├── IPv4.php       # IPv4 处理
├── IPv6.php       # IPv6 处理
└── Searcher.php   # 搜索引擎
bash
# 检查文件是否存在
ls -la db/ip2region_v*.xdb*

# 应该看到类似输出:
# -rw-r--r-- 1 user staff 10641955 May 08 10:21 db/ip2region_v4.xdb
# -rw-r--r-- 1 user staff 36086712 May 08 10:21 db/ip2region_v6.xdb