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

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

dagasmart/laravel-ip2region
===========================

ip2region 的Laravel扩展包

1.0.1(1mo ago)09↓56.3%1MITPHPPHP &gt;=7.2

Since Jun 2Pushed 1mo agoCompare

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

READMEChangelog (1)Dependencies (1)Versions (2)Used By (1)

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

[](#laravelip2region)

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

[![Latest Stable Version](https://camo.githubusercontent.com/5f1265596d1c7990fd3233e14692c489aaee4ecc41c254fda35cdf735e5917c9/687474703a2f2f706f7365722e707567782e6f72672f64616761736d6172742f6c61726176656c2d697032726567696f6e2f76)](https://packagist.org/packages/dagasmart/laravel-ip2region) [![Total Downloads](https://camo.githubusercontent.com/ace3ebb033c13f88b6c48265b9654f43232ddb4a0c660f9f88b4d5bf384332e2/687474703a2f2f706f7365722e707567782e6f72672f64616761736d6172742f6c61726176656c2d697032726567696f6e2f646f776e6c6f616473)](https://packagist.org/packages/dagasmart/laravel-ip2region) [![Latest Unstable Version](https://camo.githubusercontent.com/24d206603a5535fbba77409a0fc057a51573e6b7ba4327d32e2c74170a198068/687474703a2f2f706f7365722e707567782e6f72672f64616761736d6172742f6c61726176656c2d697032726567696f6e2f762f756e737461626c65)](https://packagist.org/packages/dagasmart/laravel-ip2region) [![License](https://camo.githubusercontent.com/bc99d21a1c9121220b09045c43861a0463027e6572dcc46e481fc639bed77470/687474703a2f2f706f7365722e707567782e6f72672f64616761736d6172742f6c61726176656c2d697032726567696f6e2f6c6963656e7365)](https://packagist.org/packages/dagasmart/laravel-ip2region) [![PHP Version Require](https://camo.githubusercontent.com/1a7d5d65ea2ddc0274d501073a7f2dafb58eb1385c09f8467bd112e928211f3c/687474703a2f2f706f7365722e707567782e6f72672f64616761736d6172742f6c61726176656c2d697032726567696f6e2f726571756972652f706870)](https://packagist.org/packages/dagasmart/laravel-ip2region)

---

要求
--

[](#要求)

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

---

安装
--

[](#安装)

```
composer require dagasmart/laravel-ip2region
```

---

开始使用
----

[](#开始使用)

1. 创建配置文件

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

2. 添加别名

```
'aliases' => [
    // ...
    "Ip2Region" => DagaSmart\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

34

—

LowBetter than 75% of packages

Maintenance90

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

53d ago

### Community

Maintainers

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

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[franzl/studio

Develop your Composer libraries with style

1.1k639.7k16](/packages/franzl-studio)[pingpong/menus

Laravel Menus

68241.3k13](/packages/pingpong-menus)[novius/laravel-nova-order-nestedset-field

A Laravel Nova field that make your resources orderable

2393.1k2](/packages/novius-laravel-nova-order-nestedset-field)[dm/ajaxcom

Controls Ajax from PHP

1516.8k](/packages/dm-ajaxcom)[hyperf-ext/sms

The Hyperf SMS package.

254.4k](/packages/hyperf-ext-sms)

PHPackages © 2026

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