Giter VIP home page Giter VIP logo

geolocation's Introduction

Geolocation Latest Stable Version Total Downloads Latest Unstable Version License Build Coverage Status

A IP Info DB integration for Laravel

Installation

This package requires PHP 5.6+, and includes a Laravel 5 Service Provider and Facade.

To install through composer include the package in your composer.json.

"midnite81/geolocation": "1.*"

Run composer install or composer update to download the dependencies or you can run composer require midnite81/geolocation.

Refresh Autoloader

At this point some users may need to run the command composer dump-autoload. Alternatively, you can run php artisan optimize which should include the dump-autoload command.

Laravel 5 Integration

To use the package with Laravel 5 firstly add the GeoLocation service provider to the list of service providers in app/config/app.php.

'providers' => [

  Midnite81\GeoLocation\GeoLocationServiceProvider::class
          
];

Add the GeoLocation facade to your aliases array.

'aliases' => [

  'GeoLocation' => Midnite81\GeoLocation\Facades\GeoLocation::class,
  
];

Publish the config and migration files using php artisan vendor:publish --provider="Midnite81\GeoLocation\GeoLocationServiceProvider"

Configuration File

Once you have published the config files, you will find a geolocation.php file in the config folder. You should look through these settings and update these where necessary.

Env

You will need to add the following to your .env file and update these with your own settings

GEOLOCATION_API_KEY=<key>
GEOLOCATION_CACHE=<duration_in_minutes>

Get your GeoLocation API Key

Before using this package you must get an API Key from IP Info DB. Please access http://ipinfodb.com/register.php and after registering and confirming your email address your api key will be show. Please copy and set to your .env file on GEOLOCATION_API_KEY option.

Example Usage

use Midnite81\GeoLocation\Contracts\Services\GeoLocation;
use Illuminate\Http\Request;

public function index(GeoLocation $geo, Request $request) 
{
    $ipLocation = $geo->getCity($request->ip());
    
    // if you do $geo->get($request->ip()), the default precision is now city

    // $ipLocation is an IpLocation Object
    
    echo $ipLocation->ipAddress; // e.g. 127.0.0.1
    
    echo $ipLocation->getAddressString(); // e.g. London, United Kingdom
    
    // the object has a toJson() and toArray() method on it 
    // so you can die and dump an array.
    dd($ipLocation->toArray()); 

}

Methods on IpLocation

$ipLocation->getStatusCode(); // returns status code of request (e.g. 200)
$ipLocation->getStatusMessage(); // returns any status message to go with code
$ipLocation->getIpAddress(); // the geolocation IP requested
$ipLocation->getCountryCode(); // country code of the IP e.g. GB
$ipLocation->getCountryName(); // country name of the IP e.g. United Kingdom
$ipLocation->getRegionName(); // region name of the IP e.g. England
$ipLocation->getCityName(); // city name of the IP e.g. London
$ipLocation->getZipCode(); // postcode of the IP e.g. SE01 1AA
$ipLocation->getPostCode(); // postcode of the IP e.g. SE01 1AA
$ipLocation->getLatitude(); // latitude of the IP e.g. 53.4030
$ipLocation->getLongitude(); // longitude of the IP e.g. -1.201
$ipLocation->getTimeZone(); // timezone of the IP e.g.
$ipLocation->getAddressString(); // gets the city, region and country as a string
$ipLocation->toArray(); // returns object as an array
$ipLocation->toJson(); // returns object as a json object

geolocation's People

Contributors

evertramos avatar lasserafn avatar midnite81 avatar mnabialek avatar

Watchers

 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.