dynamiccarrots/laravel-ip2location

本包最新版本(1.0.0)没有可用的许可证信息。

在 Laravel 中使用免费的 Ip2Location LITE 数据库

1.0.0 2017-02-23 16:33 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:52:56 UTC


README

Ip2Location 提供了一个免费的非侵入性数据库,帮助您确定访客的地理位置。

从用户的 IP 地址获得的数据将取决于您从 Ip2Location 下载的数据库大小。其中最大的包含国家、地区、城市、纬度、经度、邮编和时区。

只下载您需要的数据库,大型数据库可能超过 400 万行。最小的一个大约有 160K 行。

由于此包使用 Ip2Location 的 LITE/免费版本,您不能保证 100% 的准确性。

#安装

下载 Ip2Location 数据库

访问此页面并下载 DB1 到 DB11 之间的其中一个 DB 文件作为 CSV 文件。 http://lite.ip2location.com/

您需要在数据库目录中创建一个名为 "ip2location" 的文件夹,并将 CSV 文件存储在此处,文件名为 "ip2location.csv"。

您应该有以下结构。

    ├── database/
    │   ├── ip2location/
    │   │   ├── ip2location.csv

迁移

通过运行以下命令在 Laravel 中创建一个新的迁移文件。

# php artisan make:migration create_ip2location_table

将以下迁移复制到新创建的迁移文件中。

   /**
     * 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');
    }

运行迁移。

# php artisan migrate

创建更新命令

由于 IP 地址范围会从 ISP 到 ISP 不停地移动。如果您想保持最新,需要每月更新我们的数据库。

我们需要有一个命令允许我们更改 ip2location.csv 文件并使用其数据刷新我们的数据库。

运行以下命令。

# php artisan make:command RefreshIp2Location

这将创建一个名为 app/Console/Commands 的新文件,名为 RefreshIp2Location.php。

将此新文件中的代码替换为以下代码。

<?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;
    }
}

通过在 /app/Console/Kernel.php 中添加 RefreshIp2Location::class 到 $commands 数组中注册新命令,这将允许我们从 CLI 运行它。

    protected $commands = [
        RefreshIp2Location::class
    ];

填充数据库

现在我们已经设置了具有正确表和 csv 文件存储在正确目录的数据库,我们可以运行命令以使用 ip2location 数据填充数据库。

运行此命令。

# php artisan ip2location:refresh

使用示例

返回 IP 地址的国家代码

提供您自己的 IP 地址,Ip2Location 将将其转换为国家代码。

echo Ip2Location::lookUpIpLocation('8.8.8.8')->country_code

返回客户端国家代码

返回客户端的国家代码

echo Ip2Location::getClientsLocation()->country_code