定制 pulkitjalan/geoip 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pulkitjalan/geoip

Composer 安装命令:

composer require pulkitjalan/geoip

包简介

IP Geolocation Wrapper with Laravel Support

README 文档

README

IP Geolocation Wrapper with Laravel Support

Latest Stable Version MIT License Run Tests Coverage Total Downloads

This package provides an easy way to get geolocation information from IP addresses. It supports multiple drivers including IP-API, MaxMind Database, MaxMind API, IPStack, IP2Location, and IPinfo.

Requirements

  • PHP >= 8.2
  • Laravel 11.x or 12.x on PHP 8.2+
  • Laravel 13.x on PHP 8.3+

Installation

Install via composer:

composer require pulkitjalan/ip-geolocation

Laravel

There is a Laravel service provider and facade available.

Add the following to the providers array in your config/app.php

PulkitJalan\IPGeolocation\IPGeolocationServiceProvider::class

Next add the following to the aliases array in your config/app.php

'IPGeolocation' => PulkitJalan\IPGeolocation\Facades\IPGeolocation::class

Next publish the config file:

php artisan vendor:publish --provider="PulkitJalan\IPGeolocation\IPGeolocationServiceProvider" --tag="config"

Using an older version of PHP / Laravel?

If you are on a PHP version below 8.2 or a Laravel version below 11.0, use an older version of this package.

Usage

The ipGeolocation class takes a config array as the first parameter or defaults to using the ip-api driver.

Example:

<?php

use PulkitJalan\IPGeolocation\IPGeolocation

$ip = new IPGeolocation();

$lat = $ip->getLatitude(); // 51.5141
$lon = $ip->getLongitude(); // -3.1969

IP-API

To use the ip-api pro service you can set the options in your config.

Pro Example:

$config = [
    'driver' => 'ip-api',
    'ip-api' => [
        'key' => 'YOUR IP-API KEY',
    ],
];

Maxmind Database

To use Maxmind database as the driver you can set the options in your config.

Database Example:

$config = [
    'driver' => 'maxmind_database',
    'maxmind_database' => [
        'database' => '/path/to/database.mmdb',
    ],
];

Maxmind Api

To use Maxmind api as the driver you can set the options in your config.

Web API Example:

$config = [
    'driver' => 'maxmind_api',
    'maxmind_api' => [
        'user_id' => 'YOUR MAXMIND USER ID',
        'license_key' => 'YOUR MAXMIND LICENSE KEY'
    ],
];

IPStack

To use the ipstack as the driver set the config.

Example:

$config = [
    'driver' => 'ipstack',
    'ipstack' => [
        'key' => 'YOUR IPSTACK KEY',
        'secure' => true, // (optional) use https
    ],
];

IP2Location

To use IP2Location as the driver, set the config as follows:

Example:

$config = [
    'driver' => 'ip2location',
    'ip2location' => [
        'api_key' => 'YOUR IP2LOCATION API KEY',
    ],
];

IPinfo

To use IPinfo as the driver, set the config as follows:

Example:

$config = [
    'driver' => 'ipinfo',
    'ipinfo' => [
        'token' => 'YOUR IPINFO API TOKEN',
    ],
];

Laravel

To use this package in Laravel, simply update the config file in config/ip-geolocation.php to get the same effect. The driver can be set using the IPGEOLOCATION_DRIVER env.

Available Methods

IPGeolocation will try to determin the ip using the following http headers: HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED, HTTP_FORWARDED_FOR, HTTP_FORWARDED, REMOTE_ADDR in this order. Optionally use the setIp method to set it.

$ip->setIp('127.0.0.1');

// Laravel
IPGeolocation::setIp('127.0.0.1');

There are a number of available methods to pull out the required information. All methods will return an empty string if data is unavailable.

Get latitude

$ip->getLatitude();

// Laravel
IPGeolocation::getLatitude();

Get longitude

$ip->getLongitude();

// Laravel
IPGeolocation::getLongitude();

Get city

$ip->getCity();

// Laravel
IPGeolocation::getCity();

Get country

$ip->getCountry();

// Laravel
IPGeolocation::getCountry();

Get country code

$ip->getCountryCode();

// Laravel
IPGeolocation::getCountryCode();

Get region

$ip->getRegion();

// Laravel
IPGeolocation::getRegion();

Get region code

$ip->getRegionCode();

// Laravel
IPGeolocation::getRegionCode();

Get postal code

$ip->getPostalCode();

// Laravel
IPGeolocation::getPostalCode();

Get timezone

$ip->getTimezone();

// Laravel
IPGeolocation::getTimezone();

Get isp (not supported on all drivers)

$ip->getIsp();

// Laravel
IPGeolocation::getIsp();

Get all geo information

$ip->get(); // returns array

// Laravel
IPGeolocation::get(); // returns array

Get raw geo information

$ip->getRaw(); // different drivers will return different data types

// Laravel
IPGeolocation::getRaw(); // different drivers will return different data types

Update Database

There is an update command available to help with updating and installing a local ip geolocation database. The following will download and install/update the database file to /path/to/database.mmdb. As of 30th December 2019, Maxmind requires users to create an account and use a license key to download the databases.

<?php

use PulkitJalan\IPGeolocation\IPGeolocationUpdater

$config = [
    'driver' => 'maxmind_database',
    'maxmind_database' => [
        'database' => '/path/to/database.mmdb',
        'license_key' => 'YOUR MAXMIND LICENSE KEY'
    ],
];

(new IPGeolocationUpdater($config))->update();

Laravel

Once you have registered the service provider (supports auto discovery), you can use the command php artisan ip-geolocation:update

Services

IP-API

IP-API is a free (or paid) service that can be used instead of the database file or the paid MaxMind service. They do have some limitations on the free service, so please review their documentation first.

MaxMind

You can use the free database from MaxMind (license key required) or their web API service. You can sign up and get a free license key here.

IPStack

IPStack is a real-time IP to geolocation API service. They offer both free and paid plans. You can find more information and sign up on their website.

IP2Location

IP2Location provides IP geolocation databases and web services. They offer various products and services, including both free and paid options. You can learn more and sign up on their website.

IPinfo

IPinfo is a comprehensive IP address data provider offering accurate geolocation, ASN, company, and other IP-related information. They provide both API and database download options. You can sign up for a free API key or explore their paid plans on their website.

License

The MIT License (MIT). Please see the License File for more information.

pulkitjalan/geoip 适用场景与选型建议

pulkitjalan/geoip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 594.57k 次下载、GitHub Stars 达 91, 最近一次更新时间为 2015 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「geolocation」 「maxmind」 「ip-api」 「ipstack」 「ip-geolocation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pulkitjalan/geoip 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pulkitjalan/geoip 我们能提供哪些服务?
定制开发 / 二次开发

基于 pulkitjalan/geoip 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 594.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 95
  • 点击次数: 26
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 91
  • Watchers: 8
  • Forks: 42
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-11