PHPackages                             larva/laravel-geoip2 - 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. larva/laravel-geoip2

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

larva/laravel-geoip2
====================

This is a geoip2.

1.0.0(2y ago)057MITPHPPHP ^8.0

Since Sep 24Pushed 2y agoCompare

[ Source](https://github.com/larvatecn/laravel-geoip2)[ Packagist](https://packagist.org/packages/larva/laravel-geoip2)[ RSS](/packages/larva-laravel-geoip2/feed)WikiDiscussions master Synced 3w ago

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

laravel-geoip2
==============

[](#laravel-geoip2)

Laravel 的 geoip2 位置查询模块

[![Latest Stable Version](https://camo.githubusercontent.com/887becf2518bc5f24f7e1365d1220d6c5b106d9ac303d40535cdb912339ebd8b/68747470733a2f2f706f7365722e707567782e6f72672f6c617276612f6c61726176656c2d67656f6970322f762f737461626c652e737667)](https://packagist.org/packages/larva/laravel-geoip2)[![Latest Unstable Version](https://camo.githubusercontent.com/b9a3d2f72e71804c4f6d383b8a5d14c954224f72920611cb2d46f0c920d00c4b/68747470733a2f2f706f7365722e707567782e6f72672f6c617276612f6c61726176656c2d67656f6970322f762f756e737461626c652e737667)](https://packagist.org/packages/larva/laravel-geoip2)

简介
--

[](#简介)

- 该插件自带ip本地库，存放于`/database`； 最后更新时间：2023-08-10 **(空了就时不时更新一下)**
- `laravel-geoip2` 是基于`geoip2`作为底层，依托 `mmdb数据库` 查询ip归属地及asn信息
- 本项目基于[geoip2/geoip2](https://github.com/maxmind/GeoIP2-php)，感谢[MaxMind](https://github.com/maxmind) 工作组的开源
- 本项目基于[workbunny/webman-ip-attribution](https://github.com/workbunny/webman-ip-attribution)，感谢[workbunny](https://github.com/workbunny) 的开源
-

安装
--

[](#安装)

```
composer require larva/laravel-geoip2
```

使用
--

[](#使用)

### 配置

[](#配置)

#### 在 Laravel 中使用

[](#在-laravel-中使用)

**注：配置可选填**

```
return [
    'enable' => true,

    'default'  => '--',      // 缺省展示值
    'language' => ['zh-CN'], // 语言

    'db-country' => null,    // 自定义的country库绝对地址
    'db-city'    => null,    // 自定义的city库绝对地址
    'db-asn'     => null,    // 自定义的asn库绝对地址
];
```

### 快速获取

[](#快速获取)

```
use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
     var_dump(GeoIp2::getLocation('8.8.8.8')); // ipv4
     var_dump(GeoIp2::getLocation('::0808:0808')); // ipv6
//     [
//         'country' => 'United States',
//         'city' => '--',
//         'asn' => 'GOOGLE',
//         'continent' => 'North America',
//         'timezone' => 'America/Chicago',
//     ]
 }catch (InvalidArgumentException $exception){

 }
```

### 使用city库查询

[](#使用city库查询)

**注：City库包含了 大洲、国家、城市，但不包含网络运营商等相关信息**

```
use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
    var_dump(GeoIp2::city('8.8.8.8')); // ipv4
    var_dump(GeoIp2::city('::0808:0808')); // ipv6
    // 返回 GeoIp2\Model\City 对象

 }catch (IpAttributionException $exception){

 }
```

### 使用country库查询

[](#使用country库查询)

**注：Country库不包含城市及网络运营商等信息，通常使用City库即可，Country存在的意义在于较于City更轻**

```
use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
    var_dump(GeoIp2::country('8.8.8.8')); // ipv4
    var_dump(GeoIp2::country('::0808:0808')); // ipv6
    // 返回 GeoIp2\Model\Country 对象

 }catch (IpAttributionException $exception){

 }
```

### 使用asn库查询

[](#使用asn库查询)

**注：Asn库仅包含网络运营商等相关信息**

```
use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\InvalidArgumentException;

try {
    var_dump(GeoIp2::asn('8.8.8.8')); // ipv4
    var_dump(GeoIp2::asn('::0808:0808')); // ipv6
    // 返回 GeoIp2\Model\Asn 对象

 }catch (IpAttributionException $exception){

 }
```

### 使用原始Reader操作

[](#使用原始reader操作)

**注：原始Reader可以直接使用 [geoip2/geoip2](https://github.com/maxmind/GeoIP2-php) 提供的方法操作相关的库**

```
use Larva\GeoIp2\GeoIp2;
use Larva\GeoIp2\GeoIp2Manager;
use Larva\GeoIp2\InvalidArgumentException;

var_dump(GeoIp2::createReader(GeoIp2Manager::DB_CITY)); // City库
// 返回连接City库的 GeoIp2\Database\Reader 对象
var_dump(GeoIp2::createReader(GeoIp2Manager::DB_ASN)); // ASN库
// 返回连接ASN库的 GeoIp2\Database\Reader 对象
var_dump(GeoIp2::createReader(GeoIp2Manager::DB_ASN)); // Country库
// 返回连接Country库的 GeoIp2\Database\Reader 对象
```

更多用法和示例参照 [geoip2/geoip2](https://github.com/maxmind/GeoIP2-php)；

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

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

Unknown

Total

1

Last Release

1006d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/534cdb651e8c806590fa945c6b5a18f361613848e890db1349f4a9fbcae1a650?d=identicon)[xutongle](/maintainers/xutongle)

---

Top Contributors

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

---

Tags

laravelgeoipgeoip2

### Embed Badge

![Health badge](/badges/larva-laravel-geoip2/health.svg)

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

###  Alternatives

[stevebauman/location

Retrieve a user's location by their IP Address

1.3k8.2M84](/packages/stevebauman-location)[torann/geoip

Support for multiple Geographical Location services.

2.2k14.9M92](/packages/torann-geoip)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[vinkius-labs/laravel-page-speed

Laravel Page Speed

2.5k9.6k1](/packages/vinkius-labs-laravel-page-speed)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

19253.0k3](/packages/interaction-design-foundation-laravel-geoip)

PHPackages © 2026

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