PHPackages                             chinayin/ip2region - 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

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

chinayin/ip2region
==================

\[定期更新数据版\] 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(3y ago)616.5k↓47.4%2[1 PRs](https://github.com/chinayin/ip2region-sdk-php/pulls)2Apache-2.0PHPPHP &gt;=7.1

Since Sep 17Pushed 2y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (4)Versions (11)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/380f7d7d8ca913c0eafb775c6cc114f3b4c431f18ba76eeccb345b53ae6f19a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368696e6179696e2f697032726567696f6e2e737667)](https://packagist.org/packages/chinayin/ip2region)[![Total Downloads](https://camo.githubusercontent.com/2ff2d4cf9956d47866759bf24bbe48546fea129167ba65f7f3bcebed662f93aa/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6368696e6179696e2f697032726567696f6e2e737667)](https://packagist.org/packages/chinayin/ip2region)[![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)。

#### 定期更新数据版

[](#定期更新数据版)

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

> composer require chinayin/ip2region

#### 官方原生查询包

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

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

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

### Quick Examples

[](#quick-examples)

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

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

```
use ip2region\Ip2Region;

$ip = '1.2.3.4';
try {
    $searcher = Ip2Region::newWithFileOnly();
    $region = $searcher->search($ip);
    // 或
    $region = Ip2Region::search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

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

#### 缓存 VectorIndex 索引

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

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

```
use ip2region\Ip2Region;

$ip = '1.2.3.4';
try {
    $region = Ip2Region::newWithVectorIndex()->search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

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

#### 缓存整个 xdb 数据

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

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

```
use ip2region\Ip2Region;

$ip = '1.2.3.4';
try {
    $region = Ip2Region::newWithBuffer()->search($ip);
    var_dump($region);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}
```

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

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~127 days

Total

9

Last Release

1419d ago

Major Versions

v0.5 → 1.x-dev2021-03-09

v1.0 → 2.x-dev2022-06-30

PHP version history (2 changes)0.1PHP &gt;=5.6

2.x-devPHP &gt;=7.1

### 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 (10 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[symfony/polyfill-php56

Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions

1.6k204.0M28](/packages/symfony-polyfill-php56)[paragonie/constant_time_encoding

Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)

903329.7M148](/packages/paragonie-constant-time-encoding)[voku/portable-ascii

Portable ASCII library - performance optimized (ascii) string functions for php.

574401.5M117](/packages/voku-portable-ascii)[pear/pear_exception

The PEAR Exception base class.

9880.2M101](/packages/pear-pear-exception)[pear/pear-core-minimal

Minimal set of PEAR core files to be used as composer dependency

8073.0M70](/packages/pear-pear-core-minimal)

PHPackages © 2026

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