PHPackages                             chinayin/ip2region-core - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. chinayin/ip2region-core

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

chinayin/ip2region-core
=======================

Ip2region (2.0 - xdb) is a offline IP address manager framework and locator with ten microsecond searching performance. xdb engine implementation for many programming languages

v2.0.4(3y ago)2218.3k↓47.6%2[2 PRs](https://github.com/chinayin/ip2region-core-php/pulls)2Apache-2.0PHPPHP &gt;=7.1CI passing

Since Jun 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/chinayin/ip2region-core-php)[ Packagist](https://packagist.org/packages/chinayin/ip2region-core)[ RSS](/packages/chinayin-ip2region-core/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (9)Used By (2)

ip2region SDK for PHP
=====================

[](#ip2region-sdk-for-php)

[![Author](https://camo.githubusercontent.com/c81fe059489a5ff135b4a16cb4193135426863c4e0f69d804fde4244b5d306eb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406368696e6179696e2d626c75652e737667)](https://github.com/chinayin)[![Software License](https://camo.githubusercontent.com/1a71f03cc9974f9d8ffd61fe4a80db97f4b1ce333478e07c6b7c85f44d804609/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4170616368652d2d322e302d627269676874677265656e2e737667)](LICENSE)[![Latest Version](https://camo.githubusercontent.com/f9b197feef24f475db1cfb20cd1b97171055c0ba7a7bb297b7c747c109eb4b49/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368696e6179696e2f697032726567696f6e2d636f72652e737667)](https://packagist.org/packages/chinayin/ip2region-core)[![Total Downloads](https://camo.githubusercontent.com/8a8f58c15a3025e7676cdd4a34d58e0512b299e8cc3cd0da8672ca0a8cd0e60b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6368696e6179696e2f697032726567696f6e2d636f72652e737667)](https://packagist.org/packages/chinayin/ip2region-core)[![php 7.1+](https://camo.githubusercontent.com/ec3e91ad0853288b25e5570d62216a091b7fd2e83a46827d946a530a6cd0c542/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d6d696e253230372e312d7265642e737667)](https://camo.githubusercontent.com/ec3e91ad0853288b25e5570d62216a091b7fd2e83a46827d946a530a6cd0c542/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d6d696e253230372e312d7265642e737667)

### Installation

[](#installation)

运行环境要求 PHP 7.1 及以上版本，以及[cURL](http://php.net/manual/zh/book.curl.php)。

#### 官方原生查询包

[](#官方原生查询包)

特点：包更小，数据路径自定义

> composer require chinayin/ip2region-core

#### 包含数据查询包

[](#包含数据查询包)

特点：`xdb数据`封装在composer包内，数据会不定期更新

使用方法：[github.com/chinayin/ip2region](https://github.com/chinayin/ip2region-sdk-php)

> composer require chinayin/ip2region

### Quick Examples

[](#quick-examples)

#### 完全基于文件的查询

[](#完全基于文件的查询)

```
use ip2region\XdbSearcher;

$ip = '1.2.3.4';
$xdb = './ip2region.xdb';
try {
    $region = XdbSearcher::newWithFileOnly($xdb)->search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

> 备注：并发使用，每个线程或者协程需要创建一个独立的 searcher 对象。

#### 缓存 VectorIndex 索引

[](#缓存-vectorindex-索引)

如果你的 php 母环境支持，可以预先加载 vectorIndex 缓存，然后做成全局变量，每次创建 Searcher 的时候使用全局的 vectorIndex，可以减少一次固定的 IO 操作从而加速查询，减少 io 压力。

```
use ip2region\XdbSearcher;

$ip = '1.2.3.4';
$xdb = './ip2region.xdb';
try {
    // 1、加载 VectorIndex 缓存，把下述的 vIndex 变量缓存到内存里面。
    $vIndex = XdbSearcher::loadVectorFromFile($xdb);
    if (null === $vIndex) {
throw new \RuntimeException("failed to load vector index from '$xdb'.");
    }
    // 2、使用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。
    $searcher = XdbSearcher::newWithVectorIndex($xdb, $vIndex);
    // 3、查询
    $region = $searcher->search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

> 备注：并发使用，每个线程或者协程需要创建一个独立的 searcher 对象，但是都共享统一的只读 vectorIndex。

#### 缓存整个 xdb 数据

[](#缓存整个-xdb-数据)

如果你的 PHP 母环境支持，可以预先加载整个 xdb 的数据到内存，这样可以实现完全基于内存的查询，类似之前的 memory search 查询。

```
use ip2region\XdbSearcher;

$ip = '1.2.3.4';
$xdb = './ip2region.xdb';
try {
    // 1、加载整个 xdb 到内存。
    $cBuff = XdbSearcher::loadContentFromFile($xdb);
    if (null === $cBuff) {
        throw new \RuntimeException("failed to load content buffer from '$xdb'");
    }
    // 2、使用全局的 cBuff 创建带完全基于内存的查询对象。
    $searcher = XdbSearcher::newWithBuffer($cBuff);
    // 3、查询
    $region = $searcher->search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

> 备注：并发使用，用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~50 days

Recently: every ~63 days

Total

6

Last Release

1159d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10531946?v=4)[Leo](/maintainers/chinayin)[@chinayin](https://github.com/chinayin)

---

Top Contributors

[![chinayin](https://avatars.githubusercontent.com/u/10531946?v=4)](https://github.com/chinayin "chinayin (16 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![Rodots](https://avatars.githubusercontent.com/u/50762123?v=4)](https://github.com/Rodots "Rodots (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/chinayin-ip2region-core/health.svg)

```
[![Health](https://phpackages.com/badges/chinayin-ip2region-core/health.svg)](https://phpackages.com/packages/chinayin-ip2region-core)
```

###  Alternatives

[spatie/emoji

Display emoji characters

4671.2M32](/packages/spatie-emoji)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
