Code Monkey home page Code Monkey logo

laravel-geoip2's Introduction

laravel-geoip2

Laravel 5+ Service provider and DB downloader for Maxminds PHP API GeoIP2. https://github.com/maxmind/GeoIP2-php

License: MIT Build Status

Per Jan 2020 you now have to create an account at MaxMind and get a "license key". It is still free of charge per now.

https://dev.maxmind.com/geoip/geoip2/geolite2/

Install

In composer.json

"require": {
        "danielme85/laravel-geoip2": "dev-master",
        ....
}

or command: composer require danielme85/laravel-geoip2

Add your geoip licence to your env file:

GEOIP2_LICENSE=XXXXX

Laravel 5.x

Add to your config/app.php under Service Providers
(If you use Laravel 5.5+ you could skip this step as Autodiscovery has been enabled for this package.)

//Service Providers
danielme85\Geoip2\Geoip2ServiceProvider::class,
//Facades
'Reader'  => danielme85\Geoip2\Facade\Reader::class,

Lumen 5.x

Add to your boostrap/app.php file

$app->register(danielme85\Geoip2\Geoip2ServiceProvider::class);
...
$app->configure('app'); 
...
class_alias('danielme85\Geoip2\Facade\Reader, 'Reader');
$app->withFacades();

Config

Publish the config file to your Laravel projects

php artisan vendor:publish --provider="danielme85\Geoip2\Geoip2ServiceProvider"

The following default settings will work right away:

return [
    'geoip2' => [
        'downloadUrl' => 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz', //url db file download
        'tempFile' => 'app/GeoLite2-City.mmdb.gz', //temp download file name
        'dbName' => 'app/GeoLite2-City.mmdb', //Geoip DB filename
        'localhost' => '8.8.8.8' //when running on localhost (or for general testing) you can specify a fake ip address here.
    ]
];

Usage

You need to download the Maxmind Geoip first, the default config is for the city version (about 30MB download, 50MB extracted).

php artisan geoip:download

With the DB file downloaded you are ready to get some location data:

use danielme85\Geoip2\Facade\Reader;
...
$reader = Reader::connect();
$result = $reader->city($ip);

Usage once you have the Reader:connect object is the same as maxminds documentation https://github.com/maxmind/GeoIP2-php.

Example usage, return json location data based on ipv4 address.

<?php
use danielme85\Geoip2\Facade\Reader;
...

function getLocation(Request $request) {
   $reader = Reader::connect();
   /*
   I was experiencing inaccurate results... until I remembered that my web server traffic was routed trough CloudFlare :p
   In that case CloudFlare provides the original client ip in the following header information.
   */   
   if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
       $ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
   }
   else {
       $ip = $request->ip();
   }
   //the city() function from the GeoIp2 Php API will throw an exception if the ip-address is not found in the DB.
   try {
       $geodata = $reader->city($ip)->jsonSerialize(); //jsonSerialize seems to actually return an associative array.
   }
   catch (\Exception $e) {
       Log::warning($e->getMessage());
       return response()->json("Geo-location not found!", 500);
   }

   return response()->json($geodata);
}

This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com

laravel-geoip2's People

Contributors

danielme85 avatar dependabot[bot] avatar poinchy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.