承接 dynamiccarrots/laravel-ip2location 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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

统计信息

  • 总下载量: 23
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-02-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固