GeoIP2-php/README.md

248 lines
7.7 KiB
Markdown
Raw Normal View History

# GeoIP2 PHP API #
## Beta Note ##
This is a beta release. The API may change before the first production
release, which will be numbered 2.0.0.
You may find information on the GeoIP2 beta release process on [our
website](http://www.maxmind.com/en/geoip2_beta).
## Description ##
2013-07-16 16:21:08 +00:00
This distribution provides an API for the [GeoIP2 web services]
(http://dev.maxmind.com/geoip/geoip2/web-services) and the [GeoLite2
databases](http://dev.maxmind.com/geoip/geoip2/geolite2/). The commercial
GeoIP2 databases have not yet been released as a downloadable product.
## Installation ##
### Define Your Dependencies ###
We recommend installing this package with [Composer](http://getcomposer.org/).
2013-07-16 16:21:08 +00:00
To do this, add `geoip2/geoip2` to your `composer.json` file.
```json
{
"require": {
2013-07-16 16:21:08 +00:00
"geoip2/geoip2": "0.4.*"
}
}
```
### Install Composer ###
Run in your project root:
```
curl -s http://getcomposer.org/installer | php
```
2013-05-13 17:11:23 +00:00
### Install Dependencies ###
Run in your project root:
```
php composer.phar install
```
### Require Autoloader ###
You can autoload all dependencies by adding this to your code:
```
require 'vendor/autoload.php';
```
2013-10-21 18:03:02 +00:00
### Optional C Extension ###
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`
is returned. If the database is invalid or corrupt, a
`\MaxMind\Db\InvalidDatabaseException` will be thrown.
See the API documentation for more details.
### Example ###
```php
<?php
require_once 'vendor/autoload.php';
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
```
## Web Service Client ##
2013-07-16 16:21:08 +00:00
### Usage ###
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
corresponding to a specific end point, passing it the IP address you want to
look up.
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.
See the API documentation for more details.
2013-07-16 16:21:08 +00:00
### Example ###
```php
<?php
require_once 'vendor/autoload.php';
use GeoIp2\WebService\Client;
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.
$client = new Client(42, 'abcdef123456');
2013-09-19 14:52:44 +00:00
// Replace "city" with the method corresponding to the web service that
// you are using, e.g., "country", "cityIspOrg", "omni".
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-07-16 16:21:08 +00:00
### What data is returned? ###
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
[GeoIP2 web service docs](http://dev.maxmind.com/geoip/geoip2/web-services)
for details on what data each end point may return.
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.
Every record class attribute has a corresponding predicate method so you can
check to see if the attribute is set.
## Integration with GeoNames ##
[GeoNames](http://www.geonames.org/) offers web services and downloadable
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.
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
(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).
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
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.
## 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).
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).
## Requirements ##
This code requires PHP 5.3 or greater. Older versions of PHP are not
supported.
2013-05-13 17:24:54 +00:00
This library also relies on the [Guzzle HTTP client](http://guzzlephp.org/).
## Contributing ##
Patches and pull requests are encouraged. All code should follow the
PSR-2 style guidelines. Please include unit tests whenever possible.
## Versioning ##
The GeoIP2 PHP API uses [Semantic Versioning](http://semver.org/).
## Copyright and License ##
This software is Copyright (c) 2013 by MaxMind, Inc.
This is free software, licensed under the GNU Lesser General Public License
version 2.1 or later.