PHPackages                             sxqibo/fast-ip - 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. sxqibo/fast-ip

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

sxqibo/fast-ip
==============

根据IP获取归属地

v1.0.6(2y ago)0190MITPHP

Since Aug 9Pushed 2y ago2 watchersCompare

[ Source](https://github.com/sxqibo/fastIp)[ Packagist](https://packagist.org/packages/sxqibo/fast-ip)[ RSS](/packages/sxqibo-fast-ip/feed)WikiDiscussions main Synced 1mo ago

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

根据 IP 获取 归属地
============

[](#根据-ip-获取-归属地)

做后整理日期：2023-08-09

一、介绍
----

[](#一介绍)

本 composer 包实现了 3 个接口，分别是

- 太平洋：
- 百度：
- 阿里云：

其中，太平洋 和 百度 是免费的，阿里云 是按此收费。

太平洋 请求成功率高，但是返回的信息较少；

百度 请求频繁会限流，但是返回的信息较多；

阿里云 请求成功率高，返回的信息较多，但是按次收费。

二、使用方法
------

[](#二使用方法)

### 1、引入

[](#1引入)

引入方式是直接使用 composer 命令引入即可

```
composer require sxqibo/fast-ip
```

### 2、实例化

[](#2实例化)

实例化的方式是通过工厂方式进行实例化的

```
$obj = IpCityFactory::getIpCityObject('online');
```

该工厂通过参数获取对应的具体实现类，目前支持的参数有：

- online
- baidu
- aliyun

参数也可以为空，为空的情况下，相当于传入 online

如果没有找到传入的类，则默认返回 online 的实例

### 3、调用方法

[](#3调用方法)

调用方法有两种方式，一种方式调用实例方法，另外一种方法是调用静态方法

#### （1）调用实例方法

[](#1调用实例方法)

调用实例方法，需要通过工厂实例化后调用 `getIpCity` 方法得到 `归属地`

```
$obj = IpCityFactory::getIpCityObject('online');
$addr = $obj->getIpCity($ipAdd);
```

#### （2）调用静态方法

[](#2调用静态方法)

直接调用实现类下的静态方法即可

```
$addr = IpCityForOnline::getIpCityForStatic($ipAdd);
```

*注意：目前静态方法只有 IpCityForOnline 实现了，其他的都没有实现*

### 4、返回结果

[](#4返回结果)

返回结果是数组，数组分为两部分：

1. 一部分是 格式化 后的通用数据，其中保存了 省、市、县（区）
2. 一部分是接口返回的原始数据，原始数据字段多少各不相同，不太方便实用

其中 格式化 后的部分在 `'data'` 中，原始数据在 `'org'` 中

#### （1）举例

[](#1举例)

##### a、阿里云

[](#a阿里云)

```
{
    "data": {
        "continent": "",
        "country": "中国",
        "prov": "山西",
        "city": "大同",
        "district": "",
        "isp": "移动",
        "addr": ""
    },
    "org": {
        "data": {
            "ip": "39.144.96.133",
            "long_ip": "663773317",
            "isp": "移动",
            "area": "华北",
            "region_id": "140000",
            "region": "山西",
            "city_id": "140200",
            "city": "大同",
            "district": "",
            "district_id": "",
            "country_id": "CN",
            "country": "中国"
        },
        "ret": 200,
        "msg": "success",
        "log_id": "6dad820f8e5746649a9d1df1bc5a6e91"
    }
}
```

##### b、百度

[](#b百度)

```
{
    "data": {
        "continent": "亚洲",
        "country": "中国",
        "prov": "山西省",
        "city": "朔州市",
        "district": "",
        "isp": "中国移动",
        "addr": ""
    },
    "org": {
        "code": "Success",
        "data": {
            "continent": "亚洲",
            "country": "中国",
            "zipcode": "036000",
            "timezone": "UTC+8",
            "accuracy": "城市",
            "owner": "中国移动",
            "isp": "中国移动",
            "source": "数据挖掘",
            "areacode": "CN",
            "adcode": "140600",
            "asnumber": "56042",
            "lat": "39.330645",
            "lng": "112.466666",
            "radius": "79.8028",
            "prov": "山西省",
            "city": "朔州市",
            "district": ""
        },
        "charge": true,
        "msg": "查询成功",
        "ip": "39.144.96.133",
        "coordsys": "WGS84"
    }
}
```

##### c、太平洋

[](#c太平洋)

```
{
    "data": {
        "continent": "",
        "country": "",
        "prov": "山西省",
        "city": "",
        "district": "",
        "isp": "",
        "addr": "山西省 "
    },
    "org": {
        "ip": "39.144.96.133",
        "pro": "山西省",
        "proCode": "140000",
        "city": "",
        "cityCode": "0",
        "region": "",
        "regionCode": "0",
        "addr": "山西省 ",
        "regionNames": "",
        "err": "nocity"
    }
}
```

三、备注
----

[](#三备注)

下面是各个接口返回的数据的格式

### 1、阿里云

[](#1阿里云)

```
{
    "data": {
        "ip": "39.144.96.133",
        "long_ip": "663773317",
        "isp": "移动",
        "area": "华北",
        "region_id": "140000",
        "region": "山西",
        "city_id": "140200",
        "city": "大同",
        "district": "",
        "district_id": "",
        "country_id": "CN",
        "country": "中国"
    },
    "ret": 200,
    "msg": "success",
    "log_id": "9f1a7a9fbd7440b2aa17eacf558c30b6"
}
```

### 2、百度

[](#2百度)

```
{
    "code": "Success",
    "data": {
        "continent": "亚洲",
        "country": "中国",
        "zipcode": "036000",
        "timezone": "UTC+8",
        "accuracy": "城市",
        "owner": "中国移动",
        "isp": "中国移动",
        "source": "数据挖掘",
        "areacode": "CN",
        "adcode": "140600",
        "asnumber": "56042",
        "lat": "39.330645",
        "lng": "112.466666",
        "radius": "79.8028",
        "prov": "山西省",
        "city": "朔州市",
        "district": ""
    },
    "charge": false,
    "msg": "查询成功",
    "ip": "39.144.96.133",
    "coordsys": "WGS84"
}
```

### 3、太平洋

[](#3太平洋)

```
{
    "ip": "39.144.96.133",
    "pro": "山西省",
    "proCode": "140000",
    "city": "",
    "cityCode": "0",
    "region": "",
    "regionCode": "0",
    "addr": "山西省 ",
    "regionNames": "",
    "err": "nocity"
}
```

四、报错处理
------

[](#四报错处理)

若出现错误如下： Fatal error: Uncaught GuzzleHttp\\Exception\\RequestException: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see [https://curl.haxx.se/libcurl/…](https://curl.haxx.se/libcurl/%E2%80%A6) in xxx.php

其原因是由于本地的CURL的SSL证书太旧了，导致不识别此证书。

解决方法

1. 从  下载一个最新的证书。然后保存到一个任意目录。
2. 然后把catr.pem放到php的bin目录下，然后编辑php.ini，用记事本或者notepad++打开 php.ini文件，大概在1932行。 去掉curl.cainfo前面的注释“;”，然后在后面写上cacert.pem证书的完整路径及文件名，我的如下：
3. curl.cainfo = /Applications/EasySrv/software/php/php-8.2/bin/cacert.pem

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.8% 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 ~43 days

Recently: every ~64 days

Total

7

Last Release

757d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f32e92c7e68c3d0af13677702b745fea90913ed6173f1a0c9506f0d25a50ebe?d=identicon)[hongweizhiyuan](/maintainers/hongweizhiyuan)

![](https://www.gravatar.com/avatar/397de3883921286ae8e124fc7e0cdbe3e0b809baf12b024e8b0ecbb8f79ebd58?d=identicon)[coderup2u](/maintainers/coderup2u)

---

Top Contributors

[![coderup2u](https://avatars.githubusercontent.com/u/70255338?v=4)](https://github.com/coderup2u "coderup2u (9 commits)")[![hongweizhiyuan](https://avatars.githubusercontent.com/u/2313822?v=4)](https://github.com/hongweizhiyuan "hongweizhiyuan (2 commits)")

---

Tags

phpIP

### Embed Badge

![Health badge](/badges/sxqibo-fast-ip/health.svg)

```
[![Health](https://phpackages.com/badges/sxqibo-fast-ip/health.svg)](https://phpackages.com/packages/sxqibo-fast-ip)
```

###  Alternatives

[stevebauman/location

Retrieve a user's location by their IP Address

1.3k7.6M65](/packages/stevebauman-location)[gehrisandro/tailwind-merge-laravel

TailwindMerge for Laravel merges multiple Tailwind CSS classes by automatically resolving conflicts between them

341682.2k18](/packages/gehrisandro-tailwind-merge-laravel)

PHPackages © 2026

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