2017 © Pedro Peláez
 

library php-geolocation

PHP library that determines the country of an IP address

image

divineomega/php-geolocation

PHP library that determines the country of an IP address

  • Thursday, February 22, 2018
  • by DivineOmega
  • Repository
  • 1 Watchers
  • 1 Stars
  • 31 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

PHP Geolocation

Build Status, (*1)

This package provides a PHP library that determines the country of an IP address., (*2)

Installation

You can easily install PHP Geolocation with composer., (*3)

composer require divineomega/php-geolocation

Usage

The most simple usage of PHP Geolocation is to create a new Locator object and call its getCountryByIP method., (*4)

// Get country of the current request's IP address
$country = (new Locator)->getCountryByIP($_SERVER['REMOTE_ADDR']);

// Get country of a specific IP address
$country = (new Locator)->getCountryByIP('93.184.216.34');

// Returns a Country object
/*
object(DivineOmega\Countries\Country)#4693 (16) {
  ["name"]=>
  string(13) "United States"
  ["officialName"]=>
  string(24) "United States of America"
  // etc...
}
*/

Caching

You can configure PHP Geolocation to use any PSR-6 compliant caching library. This is easily done using the setCache method., (*5)

The following example configures a file cache (provided by the cache/filesystem-adapter package)., (*6)

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;

$filesystemAdapter = new Local(__DIR__.'/');
$filesystem = new Filesystem($filesystemAdapter);
$cachePool = new FilesystemCachePool($filesystem);

$locator = new Locator;
$locator->setCache($cachePool);

$country = $locator->getCountryByIP('93.184.216.34');

Alternative location providers

By default, PHP Geolocation will try to use the operating system's native whois command to determine the IP address. If you wish you can use an alternative location provider. This can be done using the setLocationProvider method, as follows., (*7)

$locator = new Locator;
$locator->setLocationProvider(new IpStack('my_ip_stack_api_key');

$country = $locator->getCountryByIP('93.184.216.34');

To get a free api key sign up at Ip Stack's website., (*8)

If you wish to develop your own location provider, simply create a new class that implements the LocationProviderInterface provided in this package. See the existing WhoIs and FreeGeoIP location provider classes if you need help creating your own location provider., (*9)

The Versions

22/02 2018

dev-master

9999999-dev

PHP library that determines the country of an IP address

  Sources   Download

LGPLv3 LGPL-3.0-only

The Requires

 

by Jordan Hall

22/02 2018

v1.1.1

1.1.1.0

PHP library that determines the country of an IP address

  Sources   Download

LGPL-3.0-only

The Requires

 

by Jordan Hall

19/01 2018

v1.1.0

1.1.0.0

PHP library that determines the country of an IP address

  Sources   Download

LGPLv3

The Requires

 

by Jordan Hall

22/09 2017

1.0.0

1.0.0.0

  Sources   Download

LGPLv3

The Requires

 

by Jordan Hall