Giter Site home page Giter Site logo

geoip's Introduction

GeoIP

Geoip Wrapper with Laravel Support

Latest Stable Version MIT License Build Status Run Tests Coverage StyleCI Total Downloads

Supported Drivers (Services)

Requirements

  • PHP >= 7.2

Installation

Install via composer

composer require pulkitjalan/geoip

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.

Using an older version of PHP / Laravel?

If you are on a PHP version below 7.2 or a Laravel version below 5.8 just use an older version of this package.

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

IPStack

To use the ipstack as the driver set the config.

Example:

$config = [
    'driver' => 'ipstack',
    'ipstack' => [
        'key' => 'YOUR IPSTACK KEY',
        'secure' => true, // (optional) use https
    ],
];

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',
    ],
];

Maxmind Database

To use Maxmind database as the driver you can set the options in your config.

Database Example:

$config = [
    'driver' => 'maxmind_database',
    'maxmind_database' => [
        'database' => '/path/to/database.mmdb',
    ],
];

Maxmind Api

To use Maxmind api as the driver you can set the options in your config.

Web API Example:

$config = [
    'driver' => 'maxmind_api',
    'maxmind_api' => [
        'user_id' => 'YOUR MAXMIND USER ID',
        'license_key' => 'YOUR MAXMIND LICENSE KEY'
    ],
];

Telize

To use telize as the driver set the config, and your api key.

Example:

$config = [
    'driver' => 'telize',
    'telize' => [
        'key' => 'YOUR TELIZE 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. As of 30th December 2019, Maxmind requires users to create an account and use a license key to download the databases.

<?php

use PulkitJalan\GeoIP\GeoIPUpdater

$config = [
    'driver' => 'maxmind_database',
    'maxmind_database' => [
        'database' => '/path/to/database.mmdb',
        'license_key' => 'YOUR MAXMIND LICENSE KEY'
    ],
];

(new GeoIPUpdater($config))->update();

Laravel

Once you have registered the service provider (supports auto discovery), you can use the command php artisan geoip:update

Services

IPStack

IPStack offers a JSON IP and GeoIP REST API allowing to get a visitor IP address and to query location information from any IP address.

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 (license_key required) or their web api service. You can signup and get a free license code here.

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.

geoip's People

Contributors

alberto1el avatar davidlane avatar ebisbe avatar eliepse avatar eriklotin avatar josezenem avatar lightsid avatar michellaurent avatar mweghorst avatar pulkitjalan avatar scrutinizer-auto-fixer avatar simplenotezy avatar v0ctor 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.