PHPackages                             yzh52521/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. yzh52521/ip2region

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

yzh52521/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.1(3y ago)2127↓66.7%[1 PRs](https://github.com/yzh52521/ip2region/pulls)MITPHPPHP &gt;=7.1

Since Jul 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/yzh52521/ip2region)[ Packagist](https://packagist.org/packages/yzh52521/ip2region)[ RSS](/packages/yzh52521-ip2region/feed)WikiDiscussions 2.x Synced today

READMEChangelog (4)DependenciesVersions (6)Used By (0)

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

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

### Installation

[](#installation)

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

#### 官方原生查询包

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

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

> composer require yzh52521/ip2region 2.x

#### 包含数据查询包

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

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

使用方法：[github.com/yzh52521/ip2region](https://github.com/yzh52521/ip2region)

> composer require yzh52521/ip2region 2.x

### Quick Examples

[](#quick-examples)

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

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

```
use yzh52521\XdbSearcher;

$ip = '1.2.3.4';
$xdb = "/vendor/yzh52521/ip2region/data/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 yzh52521\XdbSearcher;

$ip = '1.2.3.4';
$xdb = "/vendor/yzh52521/ip2region/data/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 yzh52521\XdbSearcher;

$ip = '1.2.3.4';
$xdb = "/vendor/yzh52521/ip2region/data/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

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

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 ~218 days

Total

5

Last Release

1248d ago

Major Versions

v1.0.1 → v2.0.02022-07-25

PHP version history (2 changes)v1.0PHP &gt;=5.4.0

v2.0.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/052aa4acd90ad5d672ae5abe8d27d1f576f9c07e1967e2f6bced03b84079310f?d=identicon)[yuanzhihai](/maintainers/yuanzhihai)

---

Top Contributors

[![yuanzhihai](https://avatars.githubusercontent.com/u/15060466?v=4)](https://github.com/yuanzhihai "yuanzhihai (25 commits)")

---

Tags

IP

### Embed Badge

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

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

###  Alternatives

[stevebauman/location

Retrieve a user's location by their IP Address

1.3k7.6M65](/packages/stevebauman-location)[s1lentium/iptools

PHP Library for manipulating network addresses (IPv4 and IPv6)

2446.2M24](/packages/s1lentium-iptools)[zhuzhichao/ip-location-zh

get the ip's location info with Chinese

617212.5k11](/packages/zhuzhichao-ip-location-zh)[markrogoyski/ipv4-subnet-calculator

Network calculator for subnet mask and other classless (CIDR) network information.

177813.7k6](/packages/markrogoyski-ipv4-subnet-calculator)[rlanvin/php-ip

IPv4/IPv6 manipulation library for PHP

180738.8k11](/packages/rlanvin-php-ip)[longman/ip-tools

PHP IP Tools for manipulation with IPv4 and IPv6

147245.6k6](/packages/longman-ip-tools)

PHPackages © 2026

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