macromindonline/geoip
Composer 安装命令:
composer require macromindonline/geoip
包简介
Geoip Wrapper with Laravel 4 & 5 Support
README 文档
README
Geoip Wrapper with Laravel 4 & 5 Support
Supported Drivers (Services)
Requirements
- PHP >= 5.5
Installation
Install via composer - edit your composer.json to require the package.
"require": { "pulkitjalan/geoip": "2.*" }
Then run composer update in your terminal to pull it in.
Laravel
There is a Laravel service provider and facade available.
Add the following to the providers array in your config/app.php
PulkitJalan\GeoIP\GeoIPServiceProvider::class
Next add the following to the aliases array in your config/app.php
'GeoIP' => PulkitJalan\GeoIP\Facades\GeoIP::class
Next run php artisan vendor:publish --provider="PulkitJalan\GeoIP\GeoIPServiceProvider" --tag="config" to publish the config file.
Looking for a Laravel 4 compatible version?
Checkout the 1.0 branch
Usage
The geoip class takes a config array as the first parameter or defaults to using the ip-api driver.
Example:
<?php use PulkitJalan\GeoIP\GeoIP $geoip = new GeoIP(); $lat = $geoip->getLatitude(); // 51.5141 $lon = $geoip->getLongitude(); // -3.1969
FreeGeoIP
To use the freegeoip as the driver set the config.
Example:
$config = [ 'driver' => 'freegeoip', 'freegeoip' => [ 'secure' => true, ], ];
Custom install example:
$config = [ 'driver' => 'freegeoip', 'freegeoip' => [ 'url' => 'freegeoip.example.com', // or with a port (freegeoip.example.com:8080) 'secure' => true, // or false ], ];
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', // optionally set secure (https) connection (default: false) 'secure' => true ], ];
Maxmind
Maxmind support the database type and also web api type.
Database Example:
$config = [ 'driver' => 'maxmind', 'maxmind' => [ 'database' => '/path/to/database.mmdb', ], ];
Web API Example:
$config = [ 'driver' => 'maxmind', 'maxmind' => [ 'user_id' => 'YOUR MAXMIND USER ID', 'license_key' => 'YOUR MAXMIND LICENSE KEY' ], ];
Telize
To use the telize as the driver set the config, and your api key.
Example:
$config = [ 'driver' => 'telize', 'telize' => [ 'key' => 'YOUR IP-API KEY', ], ];
Laravel
To use this package in Laravel, simply update the config file in config/geoip.php to get the same effect. The driver can be set using the GEOIP_DRIVER env.
Available Methods
GeoIP 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.
$geoip->setIp('127.0.0.1'); // Laravel GeoIP::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
$geoip->getLatitude(); // Laravel GeoIP::getLatitude();
Get longitude
$geoip->getLongitude(); // Laravel GeoIP::getLongitude();
Get city
$geoip->getCity(); // Laravel GeoIP::getCity();
Get country
$geoip->getCountry(); // Laravel GeoIP::getCountry();
Get country code
$geoip->getCountryCode(); // Laravel GeoIP::getCountryCode();
Get region
$geoip->getRegion(); // Laravel GeoIP::getRegion();
Get region code
$geoip->getRegionCode(); // Laravel GeoIP::getRegionCode();
Get postal code
$geoip->getPostalCode(); // Laravel GeoIP::getPostalCode();
Get timezone
$geoip->getTimezone(); // Laravel GeoIP::getTimezone();
Get isp (not supported on all drivers)
$geoip->getIsp(); // Laravel GeoIP::getIsp();
Get all geo information
$geoip->get(); // returns array // Laravel GeoIP::get(); // returns array
Get raw geo information
$geoip->getRaw(); // different drivers will return different data types // Laravel GeoIP::getRaw(); // different drivers will return different data types
Update Database
There is an update command available to help with updating and installing a local geoip database. The following will download and install/update the database file to /path/to/database.mmdb.
<?php use PulkitJalan\GeoIP\GeoIPUpdater $config = [ 'driver' => 'maxmind', 'maxmind' => [ 'database' => '/path/to/database.mmdb', ], ]; $geoipUpdater = new GeoIPUpdater($config); $geoipUpdater->update();
Laravel
Once you have registered the service provider, you can use the command php artisan geoip:update
Services
FreeGeoIP
Freegeoip is a free service that can also be used instead of the database file or the paid maxmind service. They do have some limitations so please have a look at the website first. You can also run a custom install and use that instead.
IP-API
IP-API is a free (or paid) service that can also be used instead of the database file or the paid maxmind service. They do have some limitations on the free service so please have a look at the docs first.
Maxmind
You can use the free database from maxmind or their web api service. You can download the free database service here or enter your user id and license key in the config.
Telize
Telize offers a JSON IP and GeoIP REST API allowing to get a visitor IP address and to query location information from any IP address. It outputs JSON-encoded IP geolocation data, and supports both Cross-origin resource sharing (CORS) and JSONP.
macromindonline/geoip 适用场景与选型建议
macromindonline/geoip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「maxmind」 「geoip」 「ip-api」 「telize」 「freegeoip」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 macromindonline/geoip 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 macromindonline/geoip 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 macromindonline/geoip 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A GeoIP decorator/processor for monolog
A very simple PHP library for handling http://ip-api.com/
Ipgeobase PHP API
Geolocate IP addresses using a variety of third-party services
GeoIP package for Laravel 5.
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-13