dynamiccarrots/laravel-ip2location
Composer 安装命令:
composer require dynamiccarrots/laravel-ip2location
包简介
Use the free Ip2Location LITE database within Laravel
README 文档
README
Ip2Location provides a free non-intrusive database to help you identify your visitors geographical location.
The data you receive from a users IP Address will depend on what database size you download from Ip2Location. The largest of these consists of Country, Region, City, Latitude, Longitude, Zipcode and Timezone.
Only download the database that you need, the large databases can have over 4 million rows. The smallest one has about 160K rows.
As this package uses the LITE/free version of Ip2Location you are not guaranteed 100% accuracy.
#Installation
Download Ip2Location Database
Visit this page and download one of the DB files as a CSV, these range from DB1 to DB11. http://lite.ip2location.com/
You will need to create a folder in the database directory called "ip2location" and store the CSV in here with the name "ip2location.csv".
You should have the following structure.
├── database/
│ ├── ip2location/
│ │ ├── ip2location.csv
Migrations
Create a new migration file in laravel by running the following command.
# php artisan make:migration create_ip2location_table
Copy the following migration in to the newly created migration file.
/** * Run the migrations. * * @return void */ public function up() { Schema::create('ip2location', function (Blueprint $table) { $table->integer('ip_from')->unsigned(); $table->integer('ip_to')->unsigned(); $table->char('country_code', 2); $table->string('country_name', 64)->nullable(); $table->string('region_name', 128)->nullable(); $table->string('city_name', 128)->nullable(); $table->double('latitude')->nullable(); $table->double('longitude')->nullable(); $table->string('zip_code', 30)->nullable(); $table->string('time_zone', 8)->nullable(); // Setting index $table->index('ip_from', 'idx_ip_from'); $table->index('ip_to', 'idx_ip_to'); $table->index(['ip_from', 'ip_to'], 'idx_ip_from_to'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('ip2location'); }
Run the migration.
# php artisan migrate
Create update command
Due to the fact IP address ranges get moved around from ISP to ISP all the time. We need to update our database on a monthly basis if you want to stay up to date.
We need to have a command that will allow us to change the ip2location.csv file and use its data to refresh our database.
Run the following command.
# php artisan make:command RefreshIp2Location
This will create a new file in app/Console/Commands called RefreshIp2Location.php.
Replace the code in this new file with this.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ip2Location\Ip2Location;
class RefreshIp2Location extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ip2location:refresh';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh the Ip2location table with file /database/ip2location/DB11.csv';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Ip2Location::refreshDatabase();
return;
}
}
Register the new command in /app/Console/Kernel.php by adding RefreshIp2Location::class to the $commands array, this will allow us to run this from the CLI.
protected $commands = [ RefreshIp2Location::class ];
Populate the database
Now we have a database set up with the correct table and the csv file stored in the right directory we can run the command to populate the database with the ip2location data.
Run this command.
# php artisan ip2location:refresh
Use Examples
Return the Country Code of a IP address
Provide your own IP Address and Ip2Location will convert this to a Country Code.
echo Ip2Location::lookUpIpLocation('8.8.8.8')->country_code
Return the Country Code client
Return the Country Code of the client
echo Ip2Location::getClientsLocation()->country_code
dynamiccarrots/laravel-ip2location 适用场景与选型建议
dynamiccarrots/laravel-ip2location 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dynamiccarrots/laravel-ip2location 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dynamiccarrots/laravel-ip2location 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2017-02-23