2013-05-10 18:26:04 +00:00
|
|
|
# GeoIP2 PHP API #
|
|
|
|
|
|
|
|
## Description ##
|
|
|
|
|
2013-11-15 20:18:08 +00:00
|
|
|
This package provides an API for the GeoIP2 [web services]
|
|
|
|
(http://dev.maxmind.com/geoip/geoip2/web-services) and [databases]
|
|
|
|
(http://dev.maxmind.com/geoip/geoip2/downloadable). The API also works with
|
|
|
|
the free [GeoLite2 databases](http://dev.maxmind.com/geoip/geoip2/geolite2/).
|
2013-05-10 18:26:04 +00:00
|
|
|
|
2014-04-11 23:18:14 +00:00
|
|
|
## Install via Composer ##
|
2013-05-10 19:32:02 +00:00
|
|
|
|
|
|
|
We recommend installing this package with [Composer](http://getcomposer.org/).
|
|
|
|
|
2014-07-14 19:33:59 +00:00
|
|
|
### Download Composer ###
|
2013-05-10 19:32:02 +00:00
|
|
|
|
2014-07-14 19:22:43 +00:00
|
|
|
To download Composer, run in the root directory of your project:
|
2013-05-10 19:32:02 +00:00
|
|
|
|
2014-07-14 19:22:43 +00:00
|
|
|
```bash
|
|
|
|
curl -sS https://getcomposer.org/installer | php
|
2013-05-10 19:32:02 +00:00
|
|
|
```
|
2014-07-14 19:22:43 +00:00
|
|
|
|
|
|
|
You should now have the file `composer.phar` in your project directory.
|
2013-05-10 19:32:02 +00:00
|
|
|
|
2013-05-13 17:11:23 +00:00
|
|
|
### Install Dependencies ###
|
2013-05-10 19:32:02 +00:00
|
|
|
|
|
|
|
Run in your project root:
|
|
|
|
|
|
|
|
```
|
2014-09-22 17:49:45 +00:00
|
|
|
php composer.phar require geoip2/geoip2:~2.0
|
2013-05-10 19:32:02 +00:00
|
|
|
```
|
|
|
|
|
2014-07-14 19:22:43 +00:00
|
|
|
You should now have the files `composer.json` and `composer.lock` as well as
|
|
|
|
the directory `vendor` in your project directory. If you use a version control
|
|
|
|
system, `composer.json` should be added to it.
|
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
### Require Autoloader ###
|
|
|
|
|
2014-07-14 19:22:43 +00:00
|
|
|
After installing the dependencies, you need to require the Composer autoloader
|
|
|
|
from your code:
|
|
|
|
|
|
|
|
```php
|
2013-05-10 19:32:02 +00:00
|
|
|
require 'vendor/autoload.php';
|
|
|
|
```
|
2013-10-21 18:03:02 +00:00
|
|
|
|
2014-07-14 19:33:59 +00:00
|
|
|
## Install via Phar ##
|
2014-04-11 23:18:14 +00:00
|
|
|
|
|
|
|
Although we strongly recommend using Composer, we also provide a
|
|
|
|
[phar archive](http://php.net/manual/en/book.phar.php) containing all of the
|
|
|
|
dependencies for GeoIP2. Our latest phar archive is available on
|
|
|
|
[our releases page](https://github.com/maxmind/GeoIP2-php/releases).
|
|
|
|
|
|
|
|
To use the archive, just require it from your script:
|
|
|
|
|
|
|
|
```php
|
|
|
|
require 'geoip2.phar';
|
|
|
|
```
|
|
|
|
|
|
|
|
## Optional C Extension ##
|
2013-10-21 18:03:02 +00:00
|
|
|
|
|
|
|
The [MaxMind DB API](https://github.com/maxmind/MaxMind-DB-Reader-php)
|
|
|
|
includes an optional C extension that you may install to dramatically increase
|
|
|
|
the performance of lookups in GeoIP2 or GeoLite2 databases. To install, please
|
|
|
|
follow the instructions included with that API.
|
|
|
|
|
|
|
|
The extension has no effect on web-service lookups.
|
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
## Database Reader ##
|
|
|
|
|
|
|
|
### Usage ###
|
|
|
|
|
|
|
|
To use this API, you must create a new `\GeoIp2\Database\Reader` object with
|
|
|
|
the path to the database file as the first argument to the constructor. You
|
|
|
|
may then call the method corresponding to the database you are using.
|
|
|
|
|
|
|
|
If the lookup succeeds, the method call will return a model class for the
|
|
|
|
record in the database. This model in turn contains multiple container
|
|
|
|
classes for the different parts of the data such as the city in which the
|
|
|
|
IP address is located.
|
|
|
|
|
|
|
|
If the record is not found, a `\GeoIp2\Exception\AddressNotFoundException`
|
2014-05-01 23:47:38 +00:00
|
|
|
is thrown. If the database is invalid or corrupt, a
|
2013-07-16 16:21:08 +00:00
|
|
|
`\MaxMind\Db\InvalidDatabaseException` will be thrown.
|
|
|
|
|
|
|
|
See the API documentation for more details.
|
|
|
|
|
2014-06-17 17:16:14 +00:00
|
|
|
### City Example ###
|
2013-07-16 16:21:08 +00:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
require_once 'vendor/autoload.php';
|
2013-10-16 17:27:40 +00:00
|
|
|
use GeoIp2\Database\Reader;
|
2013-07-16 16:21:08 +00:00
|
|
|
|
2013-09-19 14:52:44 +00:00
|
|
|
// This creates the Reader object, which should be reused across
|
|
|
|
// lookups.
|
2013-08-02 20:32:21 +00:00
|
|
|
$reader = new Reader('/usr/local/share/GeoIP/GeoIP2-City.mmdb');
|
2013-09-19 14:52:44 +00:00
|
|
|
|
|
|
|
// Replace "city" with the appropriate method for your database, e.g.,
|
|
|
|
// "country".
|
2013-08-02 20:32:21 +00:00
|
|
|
$record = $reader->city('128.101.101.101');
|
|
|
|
|
|
|
|
print($record->country->isoCode . "\n"); // 'US'
|
|
|
|
print($record->country->name . "\n"); // 'United States'
|
|
|
|
print($record->country->names['zh-CN'] . "\n"); // '美国'
|
|
|
|
|
|
|
|
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
|
|
|
|
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
|
|
|
|
|
|
|
|
print($record->city->name . "\n"); // 'Minneapolis'
|
|
|
|
|
|
|
|
print($record->postal->code . "\n"); // '55455'
|
|
|
|
|
|
|
|
print($record->location->latitude . "\n"); // 44.9733
|
|
|
|
print($record->location->longitude . "\n"); // -93.2323
|
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
```
|
|
|
|
|
2014-06-17 17:16:14 +00:00
|
|
|
### Connection-Type Example ###
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
require_once 'vendor/autoload.php';
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
|
|
|
|
// This creates the Reader object, which should be reused across
|
|
|
|
// lookups.
|
|
|
|
$reader = new Reader('/usr/local/share/GeoIP/GeoIP2-Connection-Type.mmdb');
|
|
|
|
|
|
|
|
$record = $reader->connectionType('128.101.101.101');
|
|
|
|
|
|
|
|
print($record->connectionType . "\n"); // 'Corporate'
|
|
|
|
print($record->ipAddress . "\n"); // '128.101.101.101'
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
### Domain Example ###
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
require_once 'vendor/autoload.php';
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
|
|
|
|
// This creates the Reader object, which should be reused across
|
|
|
|
// lookups.
|
|
|
|
$reader = new Reader('/usr/local/share/GeoIP/GeoIP2-Domain.mmdb');
|
|
|
|
|
|
|
|
$record = $reader->domain('128.101.101.101');
|
|
|
|
|
|
|
|
print($record->domain . "\n"); // 'umn.edu'
|
|
|
|
print($record->ipAddress . "\n"); // '128.101.101.101'
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
### ISP Example ###
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
require_once 'vendor/autoload.php';
|
|
|
|
use GeoIp2\Database\Reader;
|
|
|
|
|
|
|
|
// This creates the Reader object, which should be reused across
|
|
|
|
// lookups.
|
|
|
|
$reader = new Reader('/usr/local/share/GeoIP/GeoIP2-ISP.mmdb');
|
|
|
|
|
|
|
|
$record = $reader->isp('128.101.101.101');
|
|
|
|
|
|
|
|
print($record->autonomousSystemNumber . "\n"); // 217
|
|
|
|
print($record->autonomousSystemOrganization . "\n"); // 'University of Minnesota'
|
|
|
|
print($record->isp . "\n"); // 'University of Minnesota'
|
|
|
|
print($record->organization . "\n"); // 'University of Minnesota'
|
|
|
|
|
|
|
|
print($record->ipAddress . "\n"); // '128.101.101.101'
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
## Web Service Client ##
|
2013-05-10 19:32:02 +00:00
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
### Usage ###
|
2013-05-10 18:26:04 +00:00
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
To use this API, you must create a new `\GeoIp2\WebService\Client`
|
|
|
|
object with your `$userId` and `$licenseKey`, then you call the method
|
2013-05-10 19:32:02 +00:00
|
|
|
corresponding to a specific end point, passing it the IP address you want to
|
|
|
|
look up.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
If the request succeeds, the method call will return a model class for the end
|
|
|
|
point you called. This model in turn contains multiple record classes, each of
|
|
|
|
which represents part of the data returned by the web service.
|
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
If there is an error, a structured exception is thrown.
|
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
See the API documentation for more details.
|
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
### Example ###
|
2013-05-10 18:26:04 +00:00
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
require_once 'vendor/autoload.php';
|
2013-10-16 17:27:40 +00:00
|
|
|
use GeoIp2\WebService\Client;
|
2013-05-10 19:32:02 +00:00
|
|
|
|
2013-09-19 14:52:44 +00:00
|
|
|
// This creates a Client object that can be reused across requests.
|
2013-09-19 15:07:24 +00:00
|
|
|
// Replace "42" with your user ID and "license_key" with your license
|
|
|
|
// key.
|
2013-05-10 19:32:02 +00:00
|
|
|
$client = new Client(42, 'abcdef123456');
|
2013-09-19 14:52:44 +00:00
|
|
|
|
|
|
|
// Replace "city" with the method corresponding to the web service that
|
2014-07-15 21:19:30 +00:00
|
|
|
// you are using, e.g., "country", "insights".
|
2013-08-02 20:32:21 +00:00
|
|
|
$record = $client->city('128.101.101.101');
|
|
|
|
|
|
|
|
print($record->country->isoCode . "\n"); // 'US'
|
|
|
|
print($record->country->name . "\n"); // 'United States'
|
|
|
|
print($record->country->names['zh-CN'] . "\n"); // '美国'
|
|
|
|
|
|
|
|
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
|
|
|
|
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
|
|
|
|
|
|
|
|
print($record->city->name . "\n"); // 'Minneapolis'
|
|
|
|
|
|
|
|
print($record->postal->code . "\n"); // '55455'
|
|
|
|
|
|
|
|
print($record->location->latitude . "\n"); // 44.9733
|
|
|
|
print($record->location->longitude . "\n"); // -93.2323
|
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
```
|
2013-05-10 18:26:04 +00:00
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
### What data is returned? ###
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
While many of the end points return the same basic records, the attributes
|
|
|
|
which can be populated vary between end points. In addition, while an end
|
|
|
|
point may offer a particular piece of data, MaxMind does not always have every
|
|
|
|
piece of data for any given IP address.
|
|
|
|
|
|
|
|
Because of these factors, it is possible for any end point to return a record
|
|
|
|
where some or all of the attributes are unpopulated.
|
|
|
|
|
2013-05-24 21:16:43 +00:00
|
|
|
See the
|
2014-07-15 21:19:30 +00:00
|
|
|
[GeoIP2 Precision web service docs](http://dev.maxmind.com/geoip/geoip2/web-services)
|
2013-05-24 21:16:43 +00:00
|
|
|
for details on what data each end point may return.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
2013-07-16 16:21:08 +00:00
|
|
|
The only piece of data which is always returned is the `ipAddress`
|
|
|
|
attribute in the `GeoIp2\Record\Traits` record.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
## Integration with GeoNames ##
|
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
[GeoNames](http://www.geonames.org/) offers web services and downloadable
|
2013-05-10 18:26:04 +00:00
|
|
|
databases with data on geographical features around the world, including
|
|
|
|
populated places. They offer both free and paid premium data. Each
|
2013-07-16 16:21:08 +00:00
|
|
|
feature is unique identified by a `geonameId`, which is an integer.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
Many of the records returned by the GeoIP2 web services and databases
|
2013-07-16 16:21:08 +00:00
|
|
|
include a `geonameId` property. This is the ID of a geographical feature
|
2013-05-10 18:26:04 +00:00
|
|
|
(city, region, country, etc.) in the GeoNames database.
|
|
|
|
|
|
|
|
Some of the data that MaxMind provides is also sourced from GeoNames. We
|
|
|
|
source things like place names, ISO codes, and other similar data from
|
|
|
|
the GeoNames premium data set.
|
|
|
|
|
|
|
|
## Reporting data problems ##
|
|
|
|
|
|
|
|
If the problem you find is that an IP address is incorrectly mapped,
|
2013-05-24 21:16:43 +00:00
|
|
|
please
|
|
|
|
[submit your correction to MaxMind](http://www.maxmind.com/en/correction).
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
If you find some other sort of mistake, like an incorrect spelling,
|
2013-05-24 21:16:43 +00:00
|
|
|
please check the [GeoNames site](http://www.geonames.org/) first. Once
|
2013-05-10 18:26:04 +00:00
|
|
|
you've searched for a place and found it on the GeoNames map view, there
|
|
|
|
are a number of links you can use to correct data ("move", "edit",
|
|
|
|
"alternate names", etc.). Once the correction is part of the GeoNames
|
|
|
|
data set, it will be automatically incorporated into future MaxMind
|
|
|
|
releases.
|
|
|
|
|
|
|
|
If you are a paying MaxMind customer and you're not sure where to submit
|
2013-05-24 21:16:43 +00:00
|
|
|
a correction, please
|
|
|
|
[contact MaxMind support](http://www.maxmind.com/en/support) for help.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
## Other Support ##
|
|
|
|
|
2013-05-24 21:16:43 +00:00
|
|
|
Please report all issues with this code using the
|
|
|
|
[GitHub issue tracker](https://github.com/maxmind/GeoIP2-php/issues).
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
If you are having an issue with a MaxMind service that is not specific
|
2013-05-24 21:16:43 +00:00
|
|
|
to the client API, please see
|
|
|
|
[our support page](http://www.maxmind.com/en/support).
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
## Requirements ##
|
|
|
|
|
|
|
|
This code requires PHP 5.3 or greater. Older versions of PHP are not
|
|
|
|
supported.
|
|
|
|
|
2014-05-01 14:20:30 +00:00
|
|
|
This library works and is tested with HHVM.
|
|
|
|
|
2014-02-19 16:37:17 +00:00
|
|
|
This library also relies on the [Guzzle HTTP client](http://guzzlephp.org/)
|
|
|
|
and the [MaxMind DB Reader](https://github.com/maxmind/MaxMind-DB-Reader-php).
|
2013-05-10 18:26:04 +00:00
|
|
|
|
2014-08-07 13:36:04 +00:00
|
|
|
If you are using PHP 5.3 with an autoloader besides Composer, you must load
|
|
|
|
`JsonSerializable.php` in the `compat` directory.
|
|
|
|
|
2013-05-10 18:26:04 +00:00
|
|
|
## Contributing ##
|
|
|
|
|
|
|
|
Patches and pull requests are encouraged. All code should follow the
|
|
|
|
PSR-2 style guidelines. Please include unit tests whenever possible.
|
|
|
|
|
2013-05-13 17:23:02 +00:00
|
|
|
## Versioning ##
|
|
|
|
|
|
|
|
The GeoIP2 PHP API uses [Semantic Versioning](http://semver.org/).
|
|
|
|
|
2013-05-10 18:26:04 +00:00
|
|
|
## Copyright and License ##
|
|
|
|
|
2014-05-01 14:20:30 +00:00
|
|
|
This software is Copyright (c) 2014 by MaxMind, Inc.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
2013-11-01 21:39:14 +00:00
|
|
|
This is free software, licensed under the Apache License, Version 2.0.
|
|
|
|
|