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



 ip2region('61.142.118.231') . PHP_EOL;
// 中国广东省中山市【电信】

echo ip2region('61.142.118.231', 'search') . PHP_EOL;
// 中国|广东省|中山市|电信|CN

$result = ip2region('61.142.118.231', 'memory');
print_r($result);
// Array
// (
//     [city_id] => 0
//     [region] => 中国|广东省|中山市|电信|CN
// )

try {
    echo ip2region('2400:3200::1') . PHP_EOL;
} catch (Exception $e) {
    echo '查询失败: ' . $e->getMessage() . PHP_EOL;
}


rcher = new Ip2Region();

echo $searcher->simple('61.142.118.231') . PHP_EOL;
echo $searcher->search('61.142.118.231') . PHP_EOL;

$info = $searcher->getIpInfo('61.142.118.231');
print_r($info);

$batch = $searcher->batchSearch([
    '61.142.118.231',
    '114.114.114.114',
    '8.8.8.8',
]);
print_r($batch);


rcher = new Ip2Region(
    'file',
    '/absolute/path/ip2region_v4.xdb',
    '/absolute/path/ip2region_v6.xdb'
);

echo $searcher->simple('61.142.118.231') . PHP_EOL;

$searcher->setCustomDbPaths('/absolute/path/new_v4.xdb', '/absolute/path/new_v6.xdb');
print_r($searcher->getDatabaseInfo());

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

$searcher = ip2region\xdb\Searcher::newWithFileOnly(4, '/path/ip2region_v4.xdb');
$searcher = ip2region\xdb\Searcher::newWithFileOnly(ip2region\xdb\IPv4::default(), '/path/ip2region_v4.xdb');

$searcher = new Ip2Region();

echo $searcher->simple('127.0.0.1') . PHP_EOL;      // 回环地址
echo $searcher->simple('192.168.1.1') . PHP_EOL;    // 私网地址
echo $searcher->simple('169.254.1.1') . PHP_EOL;    // 链路本地地址
echo $searcher->simple('192.0.2.1') . PHP_EOL;      // 文档测试地址
echo $searcher->simple('::1') . PHP_EOL;            // 回环地址
echo $searcher->simple('2001:db8::1') . PHP_EOL;    // 文档测试地址
echo $searcher->simple('fc00::1') . PHP_EOL;        // 唯一本地地址(私网地址)

$searcher = new Ip2Region('file');

$searcher = new Ip2Region('vectorIndex');
bash
php bin/ip2down download v6
text
ip2region/
├── bin/
│   └── ip2down                     # 数据库下载管理工具
├── db/
│   └── ip2region_v4.xdb            # 内置 IPv4 数据库
├── src/
│   ├── common.php                  # ip2region() 全局函数
│   ├── Ip2Region.php               # 主查询类
│   └── ip2region/xdb/
│       ├── IPv4.php
│       ├── IPv6.php
│       ├── Searcher.php
│       └── Util.php
├── tests/
│   ├── demo.php                    # 使用演示
│   ├── quick_performance_test.php  # 性能测试
│   ├── special_addresses.php       # 特殊用途地址测试
│   └── xdb_validation.php          # XDB 校验与工厂兼容测试
├── composer.json
└── README.md