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

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

martialbe/laravel-ip2region
===========================

ip2region 的Laravel扩展包

v1.0.1(3y ago)088MITPHPPHP &gt;=7.2

Since Aug 26Pushed 3y ago1 watchersCompare

[ Source](https://github.com/MartialBE/laravel-ip2region)[ Packagist](https://packagist.org/packages/martialbe/laravel-ip2region)[ RSS](/packages/martialbe-laravel-ip2region/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

LaravelIp2region
================

[](#laravelip2region)

[ip2region](https://github.com/lionsoul2014/ip2region) 的Laravel扩展包。

[![Latest Stable Version](https://camo.githubusercontent.com/041dfa940ece351c54866848b49b5c24042309ece46f41cb91c115aca61a18c3/687474703a2f2f706f7365722e707567782e6f72672f6d61727469616c62652f6c61726176656c2d697032726567696f6e2f76)](https://packagist.org/packages/martialbe/laravel-ip2region) [![Total Downloads](https://camo.githubusercontent.com/177d8e24af7eb961f127afbf2443285035a464d3cb1b5ed45499ca5f502cbf87/687474703a2f2f706f7365722e707567782e6f72672f6d61727469616c62652f6c61726176656c2d697032726567696f6e2f646f776e6c6f616473)](https://packagist.org/packages/martialbe/laravel-ip2region) [![Latest Unstable Version](https://camo.githubusercontent.com/6fc28949d0f800b382de1da5c2db98e1f3c5a43486cb82280e1bbb81ddf7b350/687474703a2f2f706f7365722e707567782e6f72672f6d61727469616c62652f6c61726176656c2d697032726567696f6e2f762f756e737461626c65)](https://packagist.org/packages/martialbe/laravel-ip2region) [![License](https://camo.githubusercontent.com/0ed42d6f7b6146464a4c073cf43134f8decf84db72a3fd4abe8c64fd3a79dfa2/687474703a2f2f706f7365722e707567782e6f72672f6d61727469616c62652f6c61726176656c2d697032726567696f6e2f6c6963656e7365)](https://packagist.org/packages/martialbe/laravel-ip2region) [![PHP Version Require](https://camo.githubusercontent.com/feba5c589e61623c059d7ed693e75ab10b7e79921ea3d1fc5f25ef7c253c8958/687474703a2f2f706f7365722e707567782e6f72672f6d61727469616c62652f6c61726176656c2d697032726567696f6e2f726571756972652f706870)](https://packagist.org/packages/martialbe/laravel-ip2region)

---

要求
--

[](#要求)

- PHP &gt;= 7.4
- Laravel &gt;=5.8

---

安装
--

[](#安装)

```
composer require martialbe/laravel-ip2region
```

---

开始使用
----

[](#开始使用)

1. 创建配置文件

```
php artisan vendor:publish --provider="Martialbe\LaravelIp2region\ServiceProvider"
```

2. 添加别名

```
'aliases' => [
    // ...
    "Ip2Region" => Martialbe\LaravelIp2region\Facade::class,
],
```

3. 使用

```
    // array:5 [
    //     "country" => "中国"
    //     "area" => ""
    //     "state" => "上海"
    //     "city" => "上海市"
    //     "isp" => "电信"
    // ]
    \Ip2Region::ip("218.1.2.3")->toArray();

    // OR
    // 中国|0|上海|上海市|电信
    \Ip2Region::ip("218.1.2.3")->toString();

    // OR
    $region = \Ip2Region::ip("218.1.2.3");
    $region->country;
    $region->area;
    $region->state;
    $region->city;
    $region->isp;
```

4. 更新数据库

```
    php artisan ip2region:update
```

其他使用方法
------

[](#其他使用方法)

详情查看[ip2region for php](https://github.com/lionsoul2014/ip2region/tree/master/binding/php)文档

### 缓存 `VectorIndex` 索引

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

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

```
// 1、从 dbPath 加载 VectorIndex 缓存，把下述的 vIndex 变量缓存到内存里面。
$vIndex = \Ip2Region::loadVectorIndexFromFile();

// 2、使用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。
try {
    $searcher = \Ip2Region::setIndex($vIndex);
} catch (Exception $e) {
    printf("failed to create vectorIndex cached searcher with %s\n", $e);
    return;
}

// 3、查询
$region = $searcher->ip('1.2.3.4');
// 备注：并发使用，每个线程或者协程需要创建一个独立的 searcher 对象，但是都共享统一的只读 vectorIndex。
```

### 缓存整个 `xdb` 数据

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

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

```
// 1、从 dbPath 加载整个 xdb 到内存。
$cBuff = \Ip2Region::loadContentFromFile();

// 2、使用全局的 cBuff 创建带完全基于内存的查询对象。
try {
    $searcher = \Ip2Region::setDbcache($cBuff);
} catch (Exception $e) {
    printf("failed to create buffer cached searcher: %s\n", $e);
    return;
}

// 3、查询
$region = $searcher->ip('1.2.3.4');
// 备注：并发使用，用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
```

License
-------

[](#license)

MIT

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

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

Total

2

Last Release

1353d ago

PHP version history (2 changes)v1.0.0PHP &gt;=7.4

v1.0.1PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/724abe2959ebc1528c98abfd486b1b0e0725e8032cbf02ea77f550bcea793eee?d=identicon)[MartialBE](/maintainers/MartialBE)

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[mileschou/twnicip

Twnic IP

131.9k](/packages/mileschou-twnicip)

PHPackages © 2026

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