2017 © Pedro Peláez
 

library geo-ip

image

taylornetwork/geo-ip

  • Tuesday, November 29, 2016
  • by taylornetwork
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

GeoIP for Laravel

An easy way to get the location of an IP address in Laravel 5, (*1)

Install

Via Composer, (*2)

``` bash $ composer require tayornetwork/geo-ip, (*3)


## Setup Register the service provider in the providers array in `config/app.php` ``` php 'providers' => [ TaylorNetwork\GeoIP\GeoIPServiceProvider::class, ],

Add the GeoIP facade to the aliases array in config/app.php, (*4)

``` php 'aliases' => [, (*5)

'GeoIP' => TaylorNetwork\GeoIP\Facades\GeoIP::class,

],, (*6)


--- Publish Config ``` bash $ php artisan vendor:publish

Adds geoip.php to your config folder, (*7)

Basic Usage

Helper Function

This package comes with a global helper function geoip which will return call the GeoIP class for you., (*8)

Get the location of a specific IP address by passing it to geoip, (*9)

``` php geoip('8.8.8.8');, (*10)

// Returns an object, (*11)

{#xxx +"ip": "8.8.8.8", +"country_code": "US", +"country_name": "United States", +"region_code": "CA", +"region_name": "California", +"city": "Mountain View", +"zip_code": "94035", +"time_zone": "America/Los_Angeles", +"latitude": 37.386, +"longitude": -122.0838, +"metro_code": 807, }, (*12)


Calling `geoip()` with no parameters will return the location of your IP address. ### Class ``` php $geoip = new TaylorNetwork\GeoIP\GeoIP ();

Available Methods

findIP (string | null $ip)

Usage is identical to helper function (see Helper Function), (*13)

makeRequest (string | null $ip)

Returns the raw response from the HTTP request., (*14)

``` php $geoip->makeRequest('8.8.8.8');, (*15)

// Returns, (*16)

"{"ip":"8.8.8.8","country_code":"US","country_name":"United States","region_code":"CA","region_name":"California","city":"Mountain View","zip_code":"94035","time_zone":"America/Los_Angeles","latitude":37.386,"longitude":-122.0838,"metro_code":807}\n", (*17)


If called with no parameters, your IP address location is returned. ##### decode (mixed $response) Decodes the response given by `$response`. By default a JSON string is returned (see [makeRequest](#makerequest-string--null-ip)), `decode` will decode the response with `json_decode` to return an object. ``` php $response = $geoip->makeRequest('8.8.8.8'); /* * We need to remove the newline at the end of the response * or decode will fail due to invalid JSON. * * By default this is done in a driver callback function. */ $trimmed = trim($response, "\n"); $geoip->decode($trimmed); // Returns {#xxx +"ip": "8.8.8.8", +"country_code": "US", +"country_name": "United States", +"region_code": "CA", +"region_name": "California", +"city": "Mountain View", +"zip_code": "94035", +"time_zone": "America/Los_Angeles", +"latitude": 37.386, +"longitude": -122.0838, +"metro_code": 807, }
getGuzzleClient ()

Returns an instance of GuzzleHttp\Client, (*18)

getDriver ()

Returns an instance of the driver, by default FreeGeoIP, (*19)

See Drivers, (*20)

Facade

``` php GeoIP::findIP(), (*21)


All methods in the class are available with the facade. (see [Class](#class)) ## Drivers This package comes with a FreeGeoIP driver by default. However you can use any custom driver you want with this package. To create a new driver run ``` bash $ php artisan geoip:driver DriverName

This will generate a driver class in App\GeoIP\Drivers, (*22)

To use a driver, it MUST be registered in config/geoip.php, (*23)

A driver class will look something like this., (*24)

``` php namespace App\GeoIP\Drivers;, (*25)

use TaylorNetwork\GeoIP\Drivers\Driver;, (*26)

class DriverName extends Driver { /** * Driver Definition * * Set all required properties here. * * @return void */ public function define () { $this->name = 'DriverName'; $this->method = 'GET'; $this->URL = 'http://driver-url.com'; $this->responseType = 'JSON'; } }, (*27)


*All properties in the define() function must be set to a non empty value* ### Override Methods If you want to use your own function to find an IP address, or need to format the response after an HTTP request you can override these methods in your driver. #### responseCallback (mixed $response) Called after an HTTP request, but before passing to the `GeoIP` decode function (see [Decode](#decode-mixed-response)) ``` php public function responseCallback($response) { // Code to format, etc. return $response; }

findIP (string | null $ip)

Called when findIP is called from class. If false is returned, GeoIP will do the work. If not the response is then passed to responseCallback and then decode, (*28)

``` php public function findIP ($ip = null) { // Code to do HTTP request, and get response..., (*29)

return $response;

}, (*30)


### Available Methods #### property (string $key) Returns a driver property. ``` php $driver->property('name'); // Returns 'DriverName'

properties ()

Returns all driver properties in an associative array., (*31)

Credits

License

The MIT License (MIT). Please see License File for more information., (*32)

The Versions

29/11 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

by Sam Taylor

29/11 2016

0.1.1

0.1.1.0

  Sources   Download

MIT

The Requires

 

by Sam Taylor

29/11 2016

0.1.0

0.1.0.0

  Sources   Download

MIT

The Requires

 

by Sam Taylor