Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7fa971aef8 | ||
|
|
edb6940c74 | ||
|
|
57cb77a53c | ||
|
|
8aedaa5f91 | ||
|
|
87f6e2ad6b | ||
|
|
6bcd0ab21e | ||
|
|
e03b9558a9 | ||
|
|
234dcaff7c | ||
|
|
b8188bc558 | ||
|
|
8f16ed6115 | ||
|
|
8f3331002a | ||
|
|
b4b0341307 | ||
|
|
ee00329652 | ||
|
|
e8fa9a3a24 | ||
|
|
167433de0c | ||
|
|
c584dbfd6b | ||
|
|
c642a32b0c | ||
|
|
decfda7e90 | ||
|
|
3a560452af |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
_site
|
_site
|
||||||
.gh-pages
|
.gh-pages
|
||||||
|
.idea
|
||||||
composer.lock
|
composer.lock
|
||||||
composer.phar
|
composer.phar
|
||||||
phpunit.xml
|
phpunit.xml
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
0.5.0 (2013-10-21)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Renamed $languages constructor parameters to $locales for both the Client
|
||||||
|
and Reader classes.
|
||||||
|
* Documentation and code clean-up (Ben Morel).
|
||||||
|
* Added the interface `GeoIp2\ProviderInterface`, which is implemented by both
|
||||||
|
`\GeoIp2\Database\Reader` and `\GeoIp2\WebService\Client`.
|
||||||
|
|
||||||
0.4.0 (2013-07-16)
|
0.4.0 (2013-07-16)
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
|||||||
68
README.md
68
README.md
@@ -8,9 +8,6 @@ release, which will be numbered 2.0.0.
|
|||||||
You may find information on the GeoIP2 beta release process on [our
|
You may find information on the GeoIP2 beta release process on [our
|
||||||
website](http://www.maxmind.com/en/geoip2_beta).
|
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).
|
|
||||||
|
|
||||||
## Description ##
|
## Description ##
|
||||||
|
|
||||||
This distribution provides an API for the [GeoIP2 web services]
|
This distribution provides an API for the [GeoIP2 web services]
|
||||||
@@ -28,7 +25,7 @@ To do this, add `geoip2/geoip2` to your `composer.json` file.
|
|||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"geoip2/geoip2": "0.4.*"
|
"geoip2/geoip2": "0.5.*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -55,6 +52,16 @@ You can autoload all dependencies by adding this to your code:
|
|||||||
```
|
```
|
||||||
require 'vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 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.
|
||||||
|
|
||||||
## Database Reader ##
|
## Database Reader ##
|
||||||
|
|
||||||
### Usage ###
|
### Usage ###
|
||||||
@@ -79,11 +86,30 @@ See the API documentation for more details.
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
use \GeoIp2\Database\Reader;
|
use GeoIp2\Database\Reader;
|
||||||
|
|
||||||
|
// This creates the Reader object, which should be reused across
|
||||||
|
// lookups.
|
||||||
|
$reader = new Reader('/usr/local/share/GeoIP/GeoIP2-City.mmdb');
|
||||||
|
|
||||||
|
// Replace "city" with the appropriate method for your database, e.g.,
|
||||||
|
// "country".
|
||||||
|
$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
|
||||||
|
|
||||||
$reader = new Reader('/usr/local/share/GeoIP2/GeoIP2-City.mmdb');
|
|
||||||
$record = $reader->city('24.24.24.24');
|
|
||||||
print($record->country->isoCode);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Web Service Client ##
|
## Web Service Client ##
|
||||||
@@ -108,11 +134,31 @@ See the API documentation for more details.
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
use \GeoIp2\WebService\Client;
|
use GeoIp2\WebService\Client;
|
||||||
|
|
||||||
|
// This creates a Client object that can be reused across requests.
|
||||||
|
// Replace "42" with your user ID and "license_key" with your license
|
||||||
|
// key.
|
||||||
$client = new Client(42, 'abcdef123456');
|
$client = new Client(42, 'abcdef123456');
|
||||||
$omni = $client->omni('24.24.24.24');
|
|
||||||
print($omni->country->isoCode);
|
// Replace "city" with the method corresponding to the web service that
|
||||||
|
// you are using, e.g., "country", "cityIspOrg", "omni".
|
||||||
|
$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
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### What data is returned? ###
|
### What data is returned? ###
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"guzzle/guzzle": "3.*",
|
"guzzle/guzzle": "3.*",
|
||||||
"maxmind-db/reader": "0.1.*",
|
"maxmind-db/reader": "0.2.*",
|
||||||
"php": ">=5.3.1"
|
"php": ">=5.3.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -3,10 +3,7 @@
|
|||||||
namespace GeoIp2\Database;
|
namespace GeoIp2\Database;
|
||||||
|
|
||||||
use GeoIp2\Exception\AddressNotFoundException;
|
use GeoIp2\Exception\AddressNotFoundException;
|
||||||
use GeoIp2\Model\City;
|
use GeoIp2\ProviderInterface;
|
||||||
use GeoIp2\Model\CityIspOrg;
|
|
||||||
use GeoIp2\Model\Country;
|
|
||||||
use GeoIp2\Model\Omni;
|
|
||||||
use MaxMind\Db\Reader as DbReader;
|
use MaxMind\Db\Reader as DbReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,24 +35,26 @@ use MaxMind\Db\Reader as DbReader;
|
|||||||
* will be thrown.
|
* will be thrown.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class Reader
|
class Reader implements ProviderInterface
|
||||||
{
|
{
|
||||||
private $dbReader;
|
private $dbReader;
|
||||||
private $languages;
|
private $locales;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param string $filename The path to the GeoIP2 database file.
|
* @param string $filename The path to the GeoIP2 database file.
|
||||||
|
* @param array $locales List of locale codes to use in name property
|
||||||
|
* from most preferred to least preferred.
|
||||||
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
* @throws \MaxMind\Db\Reader\InvalidDatabaseException if the database
|
||||||
* is corrupt or invalid
|
* is corrupt or invalid
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$filename,
|
$filename,
|
||||||
$languages = array('en')
|
$locales = array('en')
|
||||||
) {
|
) {
|
||||||
$this->dbReader = new DbReader($filename);
|
$this->dbReader = new DbReader($filename);
|
||||||
$this->languages = $languages;
|
$this->locales = $locales;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -137,7 +136,7 @@ class Reader
|
|||||||
$record['traits']['ip_address'] = $ipAddress;
|
$record['traits']['ip_address'] = $ipAddress;
|
||||||
$class = "GeoIp2\\Model\\" . $class;
|
$class = "GeoIp2\\Model\\" . $class;
|
||||||
|
|
||||||
return new $class($record, $this->languages);
|
return new $class($record, $this->locales);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class HttpException extends GeoIp2Exception
|
|||||||
$message,
|
$message,
|
||||||
$httpStatus,
|
$httpStatus,
|
||||||
$uri,
|
$uri,
|
||||||
Exception $previous = null
|
\Exception $previous = null
|
||||||
) {
|
) {
|
||||||
$this->uri = $uri;
|
$this->uri = $uri;
|
||||||
parent::__construct($message, $httpStatus, $previous);
|
parent::__construct($message, $httpStatus, $previous);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class InvalidRequestException extends HttpException
|
|||||||
$error,
|
$error,
|
||||||
$httpStatus,
|
$httpStatus,
|
||||||
$uri,
|
$uri,
|
||||||
Exception $previous = null
|
\Exception $previous = null
|
||||||
) {
|
) {
|
||||||
$this->error = $error;
|
$this->error = $error;
|
||||||
parent::__construct($message, $httpStatus, $uri, $previous);
|
parent::__construct($message, $httpStatus, $uri, $previous);
|
||||||
|
|||||||
@@ -74,18 +74,18 @@ class City extends Country
|
|||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
public function __construct($raw, $languages)
|
public function __construct($raw, $locales)
|
||||||
{
|
{
|
||||||
parent::__construct($raw, $languages);
|
parent::__construct($raw, $locales);
|
||||||
|
|
||||||
$this->city = new \GeoIp2\Record\City($this->get('city'), $languages);
|
$this->city = new \GeoIp2\Record\City($this->get('city'), $locales);
|
||||||
$this->location = new \GeoIp2\Record\Location($this->get('location'));
|
$this->location = new \GeoIp2\Record\Location($this->get('location'));
|
||||||
$this->postal = new \GeoIp2\Record\Postal($this->get('postal'));
|
$this->postal = new \GeoIp2\Record\Postal($this->get('postal'));
|
||||||
|
|
||||||
$this->createSubdivisions($raw, $languages);
|
$this->createSubdivisions($raw, $locales);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createSubdivisions($raw, $languages)
|
private function createSubdivisions($raw, $locales)
|
||||||
{
|
{
|
||||||
if (!isset($raw['subdivisions'])) {
|
if (!isset($raw['subdivisions'])) {
|
||||||
return;
|
return;
|
||||||
@@ -94,7 +94,7 @@ class City extends Country
|
|||||||
foreach ($raw['subdivisions'] as $sub) {
|
foreach ($raw['subdivisions'] as $sub) {
|
||||||
array_push(
|
array_push(
|
||||||
$this->subdivisions,
|
$this->subdivisions,
|
||||||
new \GeoIp2\Record\Subdivision($sub, $languages)
|
new \GeoIp2\Record\Subdivision($sub, $locales)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ class City extends Country
|
|||||||
private function mostSpecificSubdivision()
|
private function mostSpecificSubdivision()
|
||||||
{
|
{
|
||||||
return empty($this->subdivisions) ?
|
return empty($this->subdivisions) ?
|
||||||
new \GeoIp2\Record\Subdivision(array(), $this->languages):
|
new \GeoIp2\Record\Subdivision(array(), $this->locales) :
|
||||||
end($this->subdivisions);
|
end($this->subdivisions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class Country
|
|||||||
{
|
{
|
||||||
private $continent;
|
private $continent;
|
||||||
private $country;
|
private $country;
|
||||||
private $languages;
|
private $locales;
|
||||||
private $maxmind;
|
private $maxmind;
|
||||||
private $registeredCountry;
|
private $registeredCountry;
|
||||||
private $representedCountry;
|
private $representedCountry;
|
||||||
@@ -47,30 +47,30 @@ class Country
|
|||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
public function __construct($raw, $languages)
|
public function __construct($raw, $locales)
|
||||||
{
|
{
|
||||||
$this->raw = $raw;
|
$this->raw = $raw;
|
||||||
|
|
||||||
$this->continent = new \GeoIp2\Record\Continent(
|
$this->continent = new \GeoIp2\Record\Continent(
|
||||||
$this->get('continent'),
|
$this->get('continent'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->country = new \GeoIp2\Record\Country(
|
$this->country = new \GeoIp2\Record\Country(
|
||||||
$this->get('country'),
|
$this->get('country'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->maxmind = new \GeoIp2\Record\MaxMind($this->get('maxmind'));
|
$this->maxmind = new \GeoIp2\Record\MaxMind($this->get('maxmind'));
|
||||||
$this->registeredCountry = new \GeoIp2\Record\Country(
|
$this->registeredCountry = new \GeoIp2\Record\Country(
|
||||||
$this->get('registered_country'),
|
$this->get('registered_country'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->representedCountry = new \GeoIp2\Record\RepresentedCountry(
|
$this->representedCountry = new \GeoIp2\Record\RepresentedCountry(
|
||||||
$this->get('represented_country'),
|
$this->get('represented_country'),
|
||||||
$languages
|
$locales
|
||||||
);
|
);
|
||||||
$this->traits = new \GeoIp2\Record\Traits($this->get('traits'));
|
$this->traits = new \GeoIp2\Record\Traits($this->get('traits'));
|
||||||
|
|
||||||
$this->languages = $languages;
|
$this->locales = $locales;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
34
src/GeoIp2/ProviderInterface.php
Normal file
34
src/GeoIp2/ProviderInterface.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2;
|
||||||
|
|
||||||
|
interface ProviderInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param ipAddress
|
||||||
|
* IPv4 or IPv6 address to lookup.
|
||||||
|
* @return \GeoIp2\Model\Country A Country model for the requested IP address.
|
||||||
|
*/
|
||||||
|
public function country($ipAddress);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ipAddress
|
||||||
|
* IPv4 or IPv6 address to lookup.
|
||||||
|
* @return \GeoIp2\Model\City A City model for the requested IP address.
|
||||||
|
*/
|
||||||
|
public function city($ipAddress);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ipAddress
|
||||||
|
* IPv4 or IPv6 address to lookup.
|
||||||
|
* @return \GeoIp2\Model\CityIspOrg A CityIspOrg model for the requested IP address.
|
||||||
|
*/
|
||||||
|
public function cityIspOrg($ipAddress);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ipAddress
|
||||||
|
* IPv4 or IPv6 address to lookup.
|
||||||
|
* @return \GeoIp2\Model\Omni An Omni model for the requested IP address.
|
||||||
|
*/
|
||||||
|
public function omni($ipAddress);
|
||||||
|
}
|
||||||
@@ -4,14 +4,14 @@ namespace GeoIp2\Record;
|
|||||||
|
|
||||||
abstract class AbstractPlaceRecord extends AbstractRecord
|
abstract class AbstractPlaceRecord extends AbstractRecord
|
||||||
{
|
{
|
||||||
private $languages;
|
private $locales;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
public function __construct($record, $languages)
|
public function __construct($record, $locales)
|
||||||
{
|
{
|
||||||
$this->languages = $languages;
|
$this->locales = $locales;
|
||||||
parent::__construct($record);
|
parent::__construct($record);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,9 +29,9 @@ abstract class AbstractPlaceRecord extends AbstractRecord
|
|||||||
|
|
||||||
private function name()
|
private function name()
|
||||||
{
|
{
|
||||||
foreach ($this->languages as $language) {
|
foreach ($this->locales as $locale) {
|
||||||
if (isset($this->names[$language])) {
|
if (isset($this->names[$locale])) {
|
||||||
return $this->names[$language];
|
return $this->names[$locale];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ namespace GeoIp2\Record;
|
|||||||
* @property int $geonameId The GeoName ID for the city. This attribute
|
* @property int $geonameId The GeoName ID for the city. This attribute
|
||||||
* is returned by all end points.
|
* is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the city based on the languages list
|
* @property string $name The name of the city based on the locales list
|
||||||
* passed to the constructor. This attribute is returned by all end points.
|
* passed to the constructor. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property array $names A array map where the keys are language codes
|
* @property array $names A array map where the keys are locale codes
|
||||||
* and the values are names. This attribute is returned by all end points.
|
* and the values are names. This attribute is returned by all end points.
|
||||||
*/
|
*/
|
||||||
class City extends AbstractPlaceRecord
|
class City extends AbstractPlaceRecord
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ namespace GeoIp2\Record;
|
|||||||
* is returned by all end points.
|
* is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name Returns the name of the continent based on the
|
* @property string $name Returns the name of the continent based on the
|
||||||
* languages list passed to the constructor. This attribute is returned by
|
* locales list passed to the constructor. This attribute is returned by
|
||||||
* all end points.
|
* all end points.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes
|
* @property array $names An array map where the keys are locale codes
|
||||||
* and the values are names. This attribute is returned by all end points.
|
* and the values are names. This attribute is returned by all end points.
|
||||||
*/
|
*/
|
||||||
class Continent extends AbstractPlaceRecord
|
class Continent extends AbstractPlaceRecord
|
||||||
|
|||||||
@@ -18,10 +18,10 @@ namespace GeoIp2\Record;
|
|||||||
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
||||||
* returned by all end points.
|
* returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the country based on the languages list
|
* @property string $name The name of the country based on the locales list
|
||||||
* passed to the constructor. This attribute is returned by all end points.
|
* passed to the constructor. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes and
|
* @property array $names An array map where the keys are locale codes and
|
||||||
* the values are names. This attribute is returned by all end points.
|
* the values are names. This attribute is returned by all end points.
|
||||||
*/
|
*/
|
||||||
class Country extends AbstractPlaceRecord
|
class Country extends AbstractPlaceRecord
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ namespace GeoIp2\Record;
|
|||||||
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
||||||
* returned by all end points.
|
* returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the country based on the languages list
|
* @property string $name The name of the country based on the locales list
|
||||||
* passed to the constructor. This attribute is returned by all end points.
|
* passed to the constructor. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes and
|
* @property array $names An array map where the keys are locale codes and
|
||||||
* the values are names. This attribute is returned by all end points.
|
* the values are names. This attribute is returned by all end points.
|
||||||
*
|
*
|
||||||
* @property string $type A string indicating the type of entity that is
|
* @property string $type A string indicating the type of entity that is
|
||||||
|
|||||||
@@ -20,11 +20,11 @@ namespace GeoIp2\Record;
|
|||||||
* http://en.wikipedia.org/wiki/ISO_3166-2 ISO 3166-2 code}. This attribute
|
* http://en.wikipedia.org/wiki/ISO_3166-2 ISO 3166-2 code}. This attribute
|
||||||
* is returned by all end points except Country.
|
* is returned by all end points except Country.
|
||||||
*
|
*
|
||||||
* @property string $name The name of the subdivision based on the languages
|
* @property string $name The name of the subdivision based on the locales
|
||||||
* list passed to the constructor. This attribute is returned by all end
|
* list passed to the constructor. This attribute is returned by all end
|
||||||
* points except Country.
|
* points except Country.
|
||||||
*
|
*
|
||||||
* @property array $names An array map where the keys are language codes and
|
* @property array $names An array map where the keys are locale codes and
|
||||||
* the values are names. This attribute is returned by all end points except
|
* the values are names. This attribute is returned by all end points except
|
||||||
* Country.
|
* Country.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,18 +2,15 @@
|
|||||||
|
|
||||||
namespace GeoIp2\WebService;
|
namespace GeoIp2\WebService;
|
||||||
|
|
||||||
use GeoIp2\Exception\GeoIp2Exception;
|
|
||||||
use GeoIp2\Exception\HttpException;
|
|
||||||
use GeoIp2\Exception\AddressNotFoundException;
|
use GeoIp2\Exception\AddressNotFoundException;
|
||||||
use GeoIp2\Exception\AuthenticationException;
|
use GeoIp2\Exception\AuthenticationException;
|
||||||
|
use GeoIp2\Exception\GeoIp2Exception;
|
||||||
|
use GeoIp2\Exception\HttpException;
|
||||||
use GeoIp2\Exception\InvalidRequestException;
|
use GeoIp2\Exception\InvalidRequestException;
|
||||||
use GeoIp2\Exception\OutOfQueriesException;
|
use GeoIp2\Exception\OutOfQueriesException;
|
||||||
use GeoIp2\Model\City;
|
use GeoIp2\ProviderInterface;
|
||||||
use GeoIp2\Model\CityIspOrg;
|
|
||||||
use GeoIp2\Model\Country;
|
|
||||||
use GeoIp2\Model\Omni;
|
|
||||||
use Guzzle\Http\Client as GuzzleClient;
|
|
||||||
use Guzzle\Common\Exception\RuntimeException;
|
use Guzzle\Common\Exception\RuntimeException;
|
||||||
|
use Guzzle\Http\Client as GuzzleClient;
|
||||||
use Guzzle\Http\Exception\ClientErrorResponseException;
|
use Guzzle\Http\Exception\ClientErrorResponseException;
|
||||||
use Guzzle\Http\Exception\ServerErrorResponseException;
|
use Guzzle\Http\Exception\ServerErrorResponseException;
|
||||||
|
|
||||||
@@ -48,11 +45,11 @@ use Guzzle\Http\Exception\ServerErrorResponseException;
|
|||||||
*
|
*
|
||||||
* If the request fails, the client class throws an exception.
|
* If the request fails, the client class throws an exception.
|
||||||
*/
|
*/
|
||||||
class Client
|
class Client implements ProviderInterface
|
||||||
{
|
{
|
||||||
private $userId;
|
private $userId;
|
||||||
private $licenseKey;
|
private $licenseKey;
|
||||||
private $languages;
|
private $locales;
|
||||||
private $host;
|
private $host;
|
||||||
private $guzzleClient;
|
private $guzzleClient;
|
||||||
|
|
||||||
@@ -61,7 +58,7 @@ class Client
|
|||||||
*
|
*
|
||||||
* @param int $userId Your MaxMind user ID
|
* @param int $userId Your MaxMind user ID
|
||||||
* @param string $licenseKey Your MaxMind license key
|
* @param string $licenseKey Your MaxMind license key
|
||||||
* @param array $languages List of language codes to use in name property
|
* @param array $locales List of locale codes to use in name property
|
||||||
* from most preferred to least preferred.
|
* from most preferred to least preferred.
|
||||||
* @param string $host Optional host parameter
|
* @param string $host Optional host parameter
|
||||||
* @param object $guzzleClient Optional Guzzle client to use (to facilitate
|
* @param object $guzzleClient Optional Guzzle client to use (to facilitate
|
||||||
@@ -70,13 +67,13 @@ class Client
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
$userId,
|
$userId,
|
||||||
$licenseKey,
|
$licenseKey,
|
||||||
$languages = array('en'),
|
$locales = array('en'),
|
||||||
$host = 'geoip.maxmind.com',
|
$host = 'geoip.maxmind.com',
|
||||||
$guzzleClient = null
|
$guzzleClient = null
|
||||||
) {
|
) {
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
$this->licenseKey = $licenseKey;
|
$this->licenseKey = $licenseKey;
|
||||||
$this->languages = $languages;
|
$this->locales = $locales;
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
// To enable unit testing
|
// To enable unit testing
|
||||||
$this->guzzleClient = $guzzleClient;
|
$this->guzzleClient = $guzzleClient;
|
||||||
@@ -95,7 +92,7 @@ class Client
|
|||||||
* provided is not in our database (e.g., a private address).
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* with the user ID or license key that you provided.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIp2\Exception\QutOfQueriesException if your account is out
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* of queries.
|
* of queries.
|
||||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
||||||
* received by the web service but is invalid for some other reason.
|
* received by the web service but is invalid for some other reason.
|
||||||
@@ -127,7 +124,7 @@ class Client
|
|||||||
* provided is not in our database (e.g., a private address).
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* with the user ID or license key that you provided.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIp2\Exception\QutOfQueriesException if your account is out
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* of queries.
|
* of queries.
|
||||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
||||||
* received by the web service but is invalid for some other reason.
|
* received by the web service but is invalid for some other reason.
|
||||||
@@ -159,7 +156,7 @@ class Client
|
|||||||
* provided is not in our database (e.g., a private address).
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* with the user ID or license key that you provided.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIp2\Exception\QutOfQueriesException if your account is out
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* of queries.
|
* of queries.
|
||||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
||||||
* received by the web service but is invalid for some other reason.
|
* received by the web service but is invalid for some other reason.
|
||||||
@@ -191,7 +188,7 @@ class Client
|
|||||||
* provided is not in our database (e.g., a private address).
|
* provided is not in our database (e.g., a private address).
|
||||||
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
* @throws \GeoIp2\Exception\AuthenticationException if there is a problem
|
||||||
* with the user ID or license key that you provided.
|
* with the user ID or license key that you provided.
|
||||||
* @throws \GeoIp2\Exception\QutOfQueriesException if your account is out
|
* @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
|
||||||
* of queries.
|
* of queries.
|
||||||
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
* @throws \GeoIp2\Exception\InvalidRequestException} if your request was
|
||||||
* received by the web service but is invalid for some other reason.
|
* received by the web service but is invalid for some other reason.
|
||||||
@@ -231,7 +228,7 @@ class Client
|
|||||||
if ($response && $response->isSuccessful()) {
|
if ($response && $response->isSuccessful()) {
|
||||||
$body = $this->handleSuccess($response, $uri);
|
$body = $this->handleSuccess($response, $uri);
|
||||||
$class = "GeoIp2\\Model\\" . $class;
|
$class = "GeoIp2\\Model\\" . $class;
|
||||||
return new $class($body, $this->languages);
|
return new $class($body, $this->locales);
|
||||||
} else {
|
} else {
|
||||||
$this->handleNon200($response, $uri);
|
$this->handleNon200($response, $uri);
|
||||||
}
|
}
|
||||||
@@ -261,8 +258,6 @@ class Client
|
|||||||
{
|
{
|
||||||
$status = $response->getStatusCode();
|
$status = $response->getStatusCode();
|
||||||
|
|
||||||
$body = array();
|
|
||||||
|
|
||||||
if ($response->getContentLength() > 0) {
|
if ($response->getContentLength() > 0) {
|
||||||
if (strstr($response->getContentType(), 'json')) {
|
if (strstr($response->getContentType(), 'json')) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use GeoIp2\Database\Reader;
|
|||||||
|
|
||||||
class ReaderTest extends \PHPUnit_Framework_TestCase
|
class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testDefaultLanguage()
|
public function testDefaultLocale()
|
||||||
{
|
{
|
||||||
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');
|
||||||
// Needed for PHP 5.3
|
// Needed for PHP 5.3
|
||||||
@@ -20,7 +20,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$reader->close();
|
$reader->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLanguageList()
|
public function testLocaleList()
|
||||||
{
|
{
|
||||||
$reader = new Reader(
|
$reader = new Reader(
|
||||||
'maxmind-db/test-data/GeoIP2-City-Test.mmdb',
|
'maxmind-db/test-data/GeoIP2-City-Test.mmdb',
|
||||||
@@ -62,7 +62,7 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException InvalidArgumentException
|
* @expectedException InvalidArgumentException
|
||||||
* @expectedExceptionMessage invalid is not a valid IP address
|
* @expectedExceptionMessage is not a valid IP address
|
||||||
*/
|
*/
|
||||||
public function testInvalidAddress()
|
public function testInvalidAddress()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -450,7 +450,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function client($response, $languages = array('en'))
|
private function client($response, $locales = array('en'))
|
||||||
{
|
{
|
||||||
$plugin = new MockPlugin();
|
$plugin = new MockPlugin();
|
||||||
$plugin->addResponse($response);
|
$plugin->addResponse($response);
|
||||||
@@ -460,7 +460,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$client = new Client(
|
$client = new Client(
|
||||||
42,
|
42,
|
||||||
'abcdef123456',
|
'abcdef123456',
|
||||||
$languages,
|
$locales,
|
||||||
'geoip.maxmind.com',
|
'geoip.maxmind.com',
|
||||||
$guzzleClient
|
$guzzleClient
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user