https://github.com/divineomega/php-geolocation
PHP library that determines the country of an IP address
https://github.com/divineomega/php-geolocation
country geolocation ip location php php-library
Last synced: 7 months ago
JSON representation
PHP library that determines the country of an IP address
- Host: GitHub
- URL: https://github.com/divineomega/php-geolocation
- Owner: DivineOmega
- License: lgpl-3.0
- Created: 2017-09-21T22:06:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-01-12T10:45:59.000Z (over 3 years ago)
- Last Synced: 2024-10-15T02:37:15.951Z (12 months ago)
- Topics: country, geolocation, ip, location, php, php-library
- Language: PHP
- Homepage:
- Size: 20.5 KB
- Stars: 8
- Watchers: 5
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Geolocation
[](https://travis-ci.com/DivineOmega/php-geolocation)
This package provides a PHP library that determines the country of an IP address.
## Installation
You can easily install PHP Geolocation with composer.
```
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.
```php
// 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.
The following example configures a file cache (provided by the `cache/filesystem-adapter` package).
```php
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.```php
$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](https://ipstack.com)._
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.