2013-05-10 18:26:04 +00:00
|
|
|
# GeoIP2 PHP API #
|
|
|
|
|
2013-06-06 16:28:51 +00:00
|
|
|
## 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).
|
|
|
|
|
|
|
|
To provide feedback or get support during the beta, please see the
|
|
|
|
[MaxMind Customer Community](https://getsatisfaction.com/maxmind).
|
2013-05-21 19:34:10 +00:00
|
|
|
|
2013-05-10 18:26:04 +00:00
|
|
|
## Description ##
|
|
|
|
|
2013-05-24 21:16:43 +00:00
|
|
|
Currently, this distribution provides an API for the [GeoIP2 web services]
|
|
|
|
(http://dev.maxmind.com/geoip/geoip2/web-services).
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
In the future, this distribution will also provide the same API for the
|
|
|
|
GeoIP2 downloadable databases. These databases have not yet been
|
|
|
|
released as a downloadable product.
|
|
|
|
|
2013-07-10 22:24:40 +00:00
|
|
|
See ``GeoIp2\WebService\Client`` for details on the web service client
|
2013-05-10 18:26:04 +00:00
|
|
|
API.
|
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
## Installation ##
|
|
|
|
|
|
|
|
### Define Your Dependencies ###
|
|
|
|
|
|
|
|
We recommend installing this package with [Composer](http://getcomposer.org/).
|
2013-05-10 19:42:03 +00:00
|
|
|
To do this, add ```geoip2/geoip2``` to your ```composer.json``` file.
|
2013-05-10 19:32:02 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"require": {
|
2013-05-29 14:43:44 +00:00
|
|
|
"geoip2/geoip2": "0.2.*"
|
2013-05-10 19:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Install Composer ###
|
|
|
|
|
|
|
|
Run in your project root:
|
|
|
|
|
|
|
|
```
|
|
|
|
curl -s http://getcomposer.org/installer | php
|
|
|
|
```
|
|
|
|
|
2013-05-13 17:11:23 +00:00
|
|
|
### Install Dependencies ###
|
2013-05-10 19:32:02 +00:00
|
|
|
|
|
|
|
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-05-10 18:26:04 +00:00
|
|
|
## Usage ##
|
|
|
|
|
2013-07-10 22:24:40 +00:00
|
|
|
To use this API, you must create a new ``\GeoIp2\WebService\Client``
|
2013-05-10 20:11:48 +00:00
|
|
|
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-05-10 19:32:02 +00:00
|
|
|
See the API documentation for more details.
|
|
|
|
|
2013-05-10 18:26:04 +00:00
|
|
|
## Example ##
|
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
require_once 'vendor/autoload.php';
|
2013-07-10 22:24:40 +00:00
|
|
|
use \GeoIp2\WebService\Client;
|
2013-05-10 19:32:02 +00:00
|
|
|
|
|
|
|
$client = new Client(42, 'abcdef123456');
|
|
|
|
$omni = $client->omni('24.24.24.24');
|
|
|
|
echo $omni->country->isoCode;
|
|
|
|
```
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
## Exceptions ##
|
|
|
|
|
|
|
|
For details on the possible errors returned by the web service itself, see
|
2013-05-24 21:16:43 +00:00
|
|
|
the
|
|
|
|
[GeoIP2 web service docs](http://dev.maxmind.com/geoip2/geoip/web-service).
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
If the web service returns an explicit error document, this is thrown as a
|
2013-07-10 22:24:40 +00:00
|
|
|
```\GeoIp2\Exception\WebServiceException```. If some other sort of transport
|
|
|
|
error occurs, this is thrown as a ```\GeoIp2\Exception\HttpException```.
|
2013-05-13 17:29:14 +00:00
|
|
|
The difference is that the web service error includes an error message and
|
2013-05-10 18:26:04 +00:00
|
|
|
error code delivered by the web service. The latter is thrown when some sort
|
|
|
|
of unanticipated error occurs, such as the web service returning a 500 or an
|
|
|
|
invalid error document.
|
|
|
|
|
|
|
|
If the web service returns any status code besides 200, 4xx, or 5xx, this also
|
2013-07-10 22:24:40 +00:00
|
|
|
becomes a ```\GeoIp2\Exception\HttpException```.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
Finally, if the web service returns a 200 but the body is invalid, the client
|
2013-07-10 22:24:40 +00:00
|
|
|
throws a ```\GeoIp2\Exception\GeoIp2Exception```.
|
2013-05-10 18:26:04 +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-05-10 18:26:04 +00:00
|
|
|
|
2013-05-10 19:32:02 +00:00
|
|
|
The only piece of data which is always returned is the ```ipAddress```
|
2013-07-10 22:24:40 +00:00
|
|
|
attribute in the ``GeoIp2\Record\Traits`` record.
|
2013-05-10 18:26:04 +00:00
|
|
|
|
|
|
|
Every record class attribute has a corresponding predicate method so you can
|
|
|
|
check to see if the attribute is set.
|
|
|
|
|
|
|
|
## 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
|
|
|
|
feature is unique identified by a ```geonameId```, which is an integer.
|
|
|
|
|
|
|
|
Many of the records returned by the GeoIP2 web services and databases
|
|
|
|
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).
|
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.
|
|
|
|
|
2013-05-13 17:24:54 +00:00
|
|
|
This library also relies on the [Guzzle HTTP client](http://guzzlephp.org/).
|
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 ##
|
|
|
|
|
|
|
|
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.
|