Rename GeoIP2 => GeoIp2 for consistency
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Exception;
|
||||
|
||||
/**
|
||||
* This class represents a generic error.
|
||||
*/
|
||||
class GeoIp2Exception extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Exception;
|
||||
|
||||
/**
|
||||
* This class represents an HTTP transport error.
|
||||
*/
|
||||
|
||||
class HttpException extends GeoIp2Exception
|
||||
{
|
||||
/**
|
||||
* The URI queried
|
||||
*/
|
||||
public $uri;
|
||||
|
||||
public function __construct(
|
||||
$message,
|
||||
$httpStatus,
|
||||
$uri,
|
||||
Exception $previous = null
|
||||
) {
|
||||
$this->uri = $uri;
|
||||
parent::__construct($message, $httpStatus, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Exception;
|
||||
|
||||
/**
|
||||
* This class represents an error returned by MaxMind's GeoIP2
|
||||
* web service.
|
||||
*/
|
||||
class WebServiceException extends HttpException
|
||||
{
|
||||
/**
|
||||
* The code returned by the MaxMind web service
|
||||
*/
|
||||
public $error;
|
||||
|
||||
public function __construct(
|
||||
$message,
|
||||
$error,
|
||||
$httpStatus,
|
||||
$uri,
|
||||
Exception $previous = null
|
||||
) {
|
||||
$this->error = $error;
|
||||
parent::__construct($message, $httpStatus, $uri, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Model;
|
||||
|
||||
/**
|
||||
* This class provides a model for the data returned by the GeoIP2
|
||||
* City end point.
|
||||
*
|
||||
* The only difference between the City, City/ISP/Org, and Omni model
|
||||
* classes is which fields in each record may be populated. See
|
||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||
*
|
||||
* @property \GeoIp2\Record\City $city City data for the requested IP
|
||||
* address.
|
||||
*
|
||||
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||
* requested IP address.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||
* IP address. This object represents the country where MaxMind believes the
|
||||
* end user is located.
|
||||
*
|
||||
* @property \GeoIp2\Record\Location $location Location data for the
|
||||
* requested IP address.
|
||||
*
|
||||
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||
* account.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||
* data for the requested IP address. This record represents the country
|
||||
* where the ISP has registered a given IP block and may differ from the
|
||||
* user's country.
|
||||
*
|
||||
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||
* Represented country data for the requested IP address. The represented
|
||||
* country is used for things like military bases or embassies. It is only
|
||||
* present when the represented country differs from the country.
|
||||
*
|
||||
* @property array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
|
||||
* objects representing the country subdivisions for the requested IP
|
||||
* address. The number and type of subdivisions varies by country, but a
|
||||
* subdivision is typically a state, province, county, etc. Subdivisions
|
||||
* are ordered from most general (largest) to most specific (smallest).
|
||||
* If the response did not contain any subdivisions, this method returns
|
||||
* an empty array.
|
||||
*
|
||||
* @property \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
|
||||
* representing the most specific subdivision returned. If the response
|
||||
* did not contain any subdivisions, this method returns an empty
|
||||
* {@link \GeoIp2\Record\Subdivision} object.
|
||||
*
|
||||
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||
* requested IP address.
|
||||
*/
|
||||
class City extends Country
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $city;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $location;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $postal;
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $subdivisions = array();
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __construct($raw, $languages)
|
||||
{
|
||||
parent::__construct($raw, $languages);
|
||||
|
||||
$this->city = new \GeoIp2\Record\City($this->get('city'), $languages);
|
||||
$this->location = new \GeoIp2\Record\Location($this->get('location'));
|
||||
$this->postal = new \GeoIp2\Record\Postal($this->get('postal'));
|
||||
|
||||
$this->createSubdivisions($raw, $languages);
|
||||
}
|
||||
|
||||
private function createSubdivisions($raw, $languages)
|
||||
{
|
||||
if (!isset($raw['subdivisions'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($raw['subdivisions'] as $sub) {
|
||||
array_push(
|
||||
$this->subdivisions,
|
||||
new \GeoIp2\Record\Subdivision($sub, $languages)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __get($attr)
|
||||
{
|
||||
if ($attr == 'mostSpecificSubdivision') {
|
||||
return $this->$attr();
|
||||
} else {
|
||||
return parent::__get($attr);
|
||||
}
|
||||
}
|
||||
|
||||
private function mostSpecificSubdivision()
|
||||
{
|
||||
return empty($this->subdivisions)?
|
||||
new \GeoIp2\Record\Subdivision(array(), $this->languages):
|
||||
end($this->subdivisions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Model;
|
||||
|
||||
/**
|
||||
* This class provides a model for the data returned by the GeoIP2
|
||||
* City/ISP/Org end point.
|
||||
*
|
||||
* The only difference between the City, City/ISP/Org, and Omni model
|
||||
* classes is which fields in each record may be populated. See
|
||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||
*
|
||||
* @property \GeoIp2\Record\City $city City data for the requested IP
|
||||
* address.
|
||||
*
|
||||
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||
* requested IP address.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||
* IP address. This object represents the country where MaxMind believes the
|
||||
* end user is located.
|
||||
*
|
||||
* @property \GeoIp2\Record\Location $location Location data for the
|
||||
* requested IP address.
|
||||
*
|
||||
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||
* account.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||
* data for the requested IP address. This record represents the country
|
||||
* where the ISP has registered a given IP block and may differ from the
|
||||
* user's country.
|
||||
*
|
||||
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||
* Represented country data for the requested IP address. The represented
|
||||
* country is used for things like military bases or embassies. It is only
|
||||
* present when the represented country differs from the country.
|
||||
*
|
||||
* @property array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
|
||||
* objects representing the country subdivisions for the requested IP
|
||||
* address. The number and type of subdivisions varies by country, but a
|
||||
* subdivision is typically a state, province, county, etc. Subdivisions
|
||||
* are ordered from most general (largest) to most specific (smallest).
|
||||
* If the response did not contain any subdivisions, this method returns
|
||||
* an empty array.
|
||||
*
|
||||
* @property \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
|
||||
* representing the most specific subdivision returned. If the response
|
||||
* did not contain any subdivisions, this method returns an empty
|
||||
* {@link \GeoIp2\Record\Subdivision} object.
|
||||
*
|
||||
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||
* requested IP address.
|
||||
*/
|
||||
class CityIspOrg extends City
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Model;
|
||||
|
||||
/**
|
||||
* This class provides a model for the data returned by the GeoIP2 Country
|
||||
* end point.
|
||||
*
|
||||
* The only difference between the City, City/ISP/Org, and Omni model
|
||||
* classes is which fields in each record may be populated. See
|
||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||
*
|
||||
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||
* requested IP address.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||
* IP address. This object represents the country where MaxMind believes the
|
||||
* end user is located.
|
||||
*
|
||||
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||
* account.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||
* data for the requested IP address. This record represents the country
|
||||
* where the ISP has registered a given IP block and may differ from the
|
||||
* user's country.
|
||||
*
|
||||
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||
* Represented country data for the requested IP address. The represented
|
||||
* country is used for things like military bases or embassies. It is only
|
||||
* present when the represented country differs from the country.
|
||||
*
|
||||
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||
* requested IP address.
|
||||
*/
|
||||
class Country
|
||||
{
|
||||
private $continent;
|
||||
private $country;
|
||||
private $languages;
|
||||
private $maxmind;
|
||||
private $registeredCountry;
|
||||
private $representedCountry;
|
||||
private $traits;
|
||||
private $raw;
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __construct($raw, $languages)
|
||||
{
|
||||
$this->raw = $raw;
|
||||
|
||||
$this->continent = new \GeoIp2\Record\Continent(
|
||||
$this->get('continent'),
|
||||
$languages
|
||||
);
|
||||
$this->country = new \GeoIp2\Record\Country(
|
||||
$this->get('country'),
|
||||
$languages
|
||||
);
|
||||
$this->maxmind = new \GeoIp2\Record\MaxMind($this->get('maxmind'));
|
||||
$this->registeredCountry = new \GeoIp2\Record\Country(
|
||||
$this->get('registered_country'),
|
||||
$languages
|
||||
);
|
||||
$this->representedCountry = new \GeoIp2\Record\RepresentedCountry(
|
||||
$this->get('represented_country'),
|
||||
$languages
|
||||
);
|
||||
$this->traits = new \GeoIp2\Record\Traits($this->get('traits'));
|
||||
|
||||
$this->languages = $languages;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected function get($field)
|
||||
{
|
||||
return isset($this->raw[$field]) ? $this->raw[$field] : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __get ($attr)
|
||||
{
|
||||
if ($attr != "instance" && isset($this->$attr)) {
|
||||
return $this->$attr;
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Unknown attribute: $attr");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Model;
|
||||
|
||||
/**
|
||||
* This class provides a model for the data returned by the GeoIP2
|
||||
* Omni end point.
|
||||
*
|
||||
* The only difference between the City, City/ISP/Org, and Omni model
|
||||
* classes is which fields in each record may be populated. See
|
||||
* http://dev.maxmind.com/geoip/geoip2/web-services more details.
|
||||
*
|
||||
* @property \GeoIp2\Record\City $city City data for the requested IP
|
||||
* address.
|
||||
*
|
||||
* @property \GeoIp2\Record\Continent $continent Continent data for the
|
||||
* requested IP address.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $country Country data for the requested
|
||||
* IP address. This object represents the country where MaxMind believes the
|
||||
* end user is located.
|
||||
*
|
||||
* @property \GeoIp2\Record\Location $location Location data for the
|
||||
* requested IP address.
|
||||
*
|
||||
* @property \GeoIp2\Record\MaxMind $maxmind Data related to your MaxMind
|
||||
* account.
|
||||
*
|
||||
* @property \GeoIp2\Record\Country $registeredCountry Registered country
|
||||
* data for the requested IP address. This record represents the country
|
||||
* where the ISP has registered a given IP block and may differ from the
|
||||
* user's country.
|
||||
*
|
||||
* @property \GeoIp2\Record\RepresentedCountry $representedCountry
|
||||
* Represented country data for the requested IP address. The represented
|
||||
* country is used for things like military bases or embassies. It is only
|
||||
* present when the represented country differs from the country.
|
||||
*
|
||||
* @property array $subdivisions An array of {@link \GeoIp2\Record\Subdivision}
|
||||
* objects representing the country subdivisions for the requested IP
|
||||
* address. The number and type of subdivisions varies by country, but a
|
||||
* subdivision is typically a state, province, county, etc. Subdivisions
|
||||
* are ordered from most general (largest) to most specific (smallest).
|
||||
* If the response did not contain any subdivisions, this method returns
|
||||
* an empty array.
|
||||
*
|
||||
* @property \GeoIp2\Record\Subdivision $mostSpecificSubdivision An object
|
||||
* representing the most specific subdivision returned. If the response
|
||||
* did not contain any subdivisions, this method returns an empty
|
||||
* {@link \GeoIp2\Record\Subdivision} object.
|
||||
*
|
||||
* @property \GeoIp2\Record\Traits $traits Data for the traits of the
|
||||
* requested IP address.
|
||||
*/
|
||||
class Omni extends CityIspOrg
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
abstract class AbstractPlaceRecord extends AbstractRecord
|
||||
{
|
||||
private $languages;
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __construct($record, $languages)
|
||||
{
|
||||
$this->languages = $languages;
|
||||
parent::__construct($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __get($attr)
|
||||
{
|
||||
if ($attr == 'name') {
|
||||
return $this->name();
|
||||
} else {
|
||||
return parent::__get($attr);
|
||||
}
|
||||
}
|
||||
|
||||
private function name()
|
||||
{
|
||||
foreach ($this->languages as $language) {
|
||||
if (isset($this->names[$language])) {
|
||||
return $this->names[$language];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
abstract class AbstractRecord
|
||||
{
|
||||
private $record;
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __construct($record)
|
||||
{
|
||||
$this->record = $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
public function __get($attr)
|
||||
{
|
||||
$valid = in_array($attr, $this->validAttributes);
|
||||
// XXX - kind of ugly but greatly reduces boilerplate code
|
||||
$key = strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
|
||||
|
||||
if ($valid && isset($this->record[$key])) {
|
||||
return $this->record[$key];
|
||||
} elseif ($valid) {
|
||||
return null;
|
||||
} else {
|
||||
throw new \RuntimeException("Unknown attribute: $attr");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
* City-level data associated with an IP address.
|
||||
*
|
||||
* This record is returned by all the end points except the Country end point.
|
||||
*
|
||||
* @property int $confidence A value from 0-100 indicating MaxMind's
|
||||
* confidence that the city is correct. This attribute is only available
|
||||
* from the Omni end point.
|
||||
*
|
||||
* @property int $geonameId The GeoName ID for the city. This attribute
|
||||
* is returned by all end points.
|
||||
*
|
||||
* @property string $name The name of the city based on the languages list
|
||||
* passed to the constructor. This attribute is returned by all end points.
|
||||
*
|
||||
* @property array $names A array map where the keys are language codes
|
||||
* and the values are names. This attribute is returned by all end points.
|
||||
*/
|
||||
class City extends AbstractPlaceRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array('confidence', 'geonameId', 'names');
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
* Contains data for the continent record associated with an IP address
|
||||
*
|
||||
* This record is returned by all the end points.
|
||||
*
|
||||
* @property string $code A two character continent code like "NA" (North
|
||||
* America) or "OC" (Oceania). This attribute is returned by all end points.
|
||||
*
|
||||
* @property int $geonameId The GeoName ID for the continent. This attribute
|
||||
* is returned by all end points.
|
||||
*
|
||||
* @property string $name Returns the name of the continent based on the
|
||||
* languages list 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 the values are names. This attribute is returned by all end points.
|
||||
*/
|
||||
class Continent extends AbstractPlaceRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array(
|
||||
'code',
|
||||
'geonameId',
|
||||
'names'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
* Contains data for the country record associated with an IP address
|
||||
*
|
||||
* This record is returned by all the end points.
|
||||
*
|
||||
* @property int $confidence A value from 0-100 indicating MaxMind's
|
||||
* confidence that the country is correct. This attribute is only available
|
||||
* from the Omni end point.
|
||||
*
|
||||
* @property int $geonameId The GeoName ID for the country. This attribute is
|
||||
* returned by all end points.
|
||||
*
|
||||
* @property string $isoCode The {@link http://en.wikipedia.org/wiki/ISO_3166-1
|
||||
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
||||
* returned by all end points.
|
||||
*
|
||||
* @property string $name The name of the country based on the languages list
|
||||
* 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
|
||||
* the values are names. This attribute is returned by all end points.
|
||||
*/
|
||||
class Country extends AbstractPlaceRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array(
|
||||
'confidence',
|
||||
'geonameId',
|
||||
'isoCode',
|
||||
'names'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
* Contains data for the location record associated with an IP address
|
||||
*
|
||||
* This record is returned by all the end points except the Country end point.
|
||||
*
|
||||
* @property int $accuracyRadius The radius in kilometers around the
|
||||
* specified location where the IP address is likely to be. This attribute
|
||||
* is only available from the Omni end point.
|
||||
*
|
||||
* @property float $latitude The latitude of the location as a floating
|
||||
* point number. This attribute is returned by all end points except the
|
||||
* Country end point.
|
||||
*
|
||||
* @property float $longitude The longitude of the location as a
|
||||
* floating point number. This attribute is returned by all end points
|
||||
* except the Country end point.
|
||||
*
|
||||
* @property int $metroCode The metro code of the location if the location
|
||||
* is in the US. MaxMind returns the same metro codes as the
|
||||
* {@link
|
||||
* https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions
|
||||
* Google AdWords API}. This attribute is returned by all end points except
|
||||
* the Country end point.
|
||||
*
|
||||
* @property string $timeZone The time zone associated with location, as
|
||||
* specified by the {@link http://www.iana.org/time-zones IANA Time Zone
|
||||
* Database}, e.g., "America/New_York". This attribute is returned by all
|
||||
* end points except the Country end point.
|
||||
*/
|
||||
class Location extends AbstractRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array(
|
||||
'accuracyRadius',
|
||||
'latitude',
|
||||
'longitude',
|
||||
'metroCode',
|
||||
'postalCode',
|
||||
'postalConfidence',
|
||||
'timeZone'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
* Contains data about your account.
|
||||
*
|
||||
* This record is returned by all the end points.
|
||||
*
|
||||
* @property int $queriesRemaining The number of remaining queries you have
|
||||
* for the end point you are calling.
|
||||
*/
|
||||
class MaxMind extends AbstractRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array('queriesRemaining');
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
* Contains data for the postal record associated with an IP address
|
||||
*
|
||||
* This record is returned by all the end points except the Country end point.
|
||||
*
|
||||
* @property string $code The postal code of the location. Postal codes are
|
||||
* not available for all countries. In some countries, this will only contain
|
||||
* part of the postal code. This attribute is returned by all end points
|
||||
* except the Country end point.
|
||||
*
|
||||
* @property int $confidence A value from 0-100 indicating MaxMind's
|
||||
* confidence that the postal code is correct. This attribute is only
|
||||
* available from the Omni end point.
|
||||
*/
|
||||
class Postal extends AbstractRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array('code', 'confidence');
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
* Contains data for the represented country associated with an IP address
|
||||
*
|
||||
* This class contains the country-level data associated with an IP address
|
||||
* for the IP's represented country. The represented country is the country
|
||||
* represented by something like a military base or embassy.
|
||||
*
|
||||
* This record is returned by all the end points.
|
||||
*
|
||||
* @property int $confidence A value from 0-100 indicating MaxMind's
|
||||
* confidence that the country is correct. This attribute is only available
|
||||
* from the Omni end point.
|
||||
*
|
||||
* @property int $geonameId The GeoName ID for the country. This attribute is
|
||||
* returned by all end points.
|
||||
*
|
||||
* @property string $isoCode The {@link http://en.wikipedia.org/wiki/ISO_3166-1
|
||||
* two-character ISO 3166-1 alpha code} for the country. This attribute is
|
||||
* returned by all end points.
|
||||
*
|
||||
* @property string $name The name of the country based on the languages list
|
||||
* 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
|
||||
* the values are names. This attribute is returned by all end points.
|
||||
*
|
||||
* @property string $type A string indicating the type of entity that is
|
||||
* representing the country. Currently we only return <code>military</code>
|
||||
* but this could expand to include other types such as <code>embassy</code>
|
||||
* in the future. Returned by all endpoints.
|
||||
*/
|
||||
class RepresentedCountry extends Country
|
||||
{
|
||||
protected $validAttributes = array(
|
||||
'confidence',
|
||||
'geonameId',
|
||||
'isoCode',
|
||||
'namespace',
|
||||
'type'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
*
|
||||
* Contains data for the subdivisions associated with an IP address
|
||||
*
|
||||
* This record is returned by all the end points except the Country end point.
|
||||
*
|
||||
* @property int $confidence This is a value from 0-100 indicating MaxMind's
|
||||
* confidence that the subdivision is correct. This attribute is only
|
||||
* available from the Omni end point.
|
||||
*
|
||||
* @property int $geonameId This is a GeoName ID for the subdivision. This
|
||||
* attribute is returned by all end points except Country.
|
||||
*
|
||||
* @property string $isoCode This is a string up to three characters long
|
||||
* contain the subdivision portion of the {@link
|
||||
* http://en.wikipedia.org/wiki/ISO_3166-2 ISO 3166-2 code}. This attribute
|
||||
* is returned by all end points except Country.
|
||||
*
|
||||
* @property string $name The name of the subdivision based on the languages
|
||||
* list passed to the constructor. This attribute is returned by all end
|
||||
* points except Country.
|
||||
*
|
||||
* @property array $names An array map where the keys are language codes and
|
||||
* the values are names. This attribute is returned by all end points except
|
||||
* Country.
|
||||
*/
|
||||
class Subdivision extends AbstractPlaceRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array(
|
||||
'confidence',
|
||||
'geonameId',
|
||||
'isoCode',
|
||||
'names'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Record;
|
||||
|
||||
/**
|
||||
*
|
||||
* Contains data for the traits record associated with an IP address
|
||||
*
|
||||
* This record is returned by all the end points.
|
||||
*
|
||||
* @property int $autonomousSystemNumber The {@link
|
||||
* http://en.wikipedia.org/wiki/Autonomous_system_(Internet) autonomous
|
||||
* system number} associated with the IP address. This attribute is only
|
||||
* available from the City/ISP/Org and Omni end points.
|
||||
*
|
||||
* @property string $autonomousSystemOrganization The organization
|
||||
* associated with the registered {@link
|
||||
* http://en.wikipedia.org/wiki/Autonomous_system_(Internet) autonomous
|
||||
* system number} for the IP address. This attribute is only available from
|
||||
* the City/ISP/Org and Omni end points.
|
||||
*
|
||||
* @property string $domain The second level domain associated with the
|
||||
* IP address. This will be something like "example.com" or "example.co.uk",
|
||||
* not "foo.example.com". This attribute is only available from the
|
||||
* City/ISP/Org and Omni end points.
|
||||
*
|
||||
* @property string $ipAddress The IP address that the data in the model
|
||||
* is for. If you performed a "me" lookup against the web service, this
|
||||
* will be the externally routable IP address for the system the code is
|
||||
* running on. If the system is behind a NAT, this may differ from the IP
|
||||
* address locally assigned to it. This attribute is returned by all end
|
||||
* points.
|
||||
*
|
||||
* @property boolean $isAnonymousProxy This is true if the IP is an
|
||||
* anonymous proxy. See {@link http://dev.maxmind.com/faq/geoip#anonproxy}
|
||||
* for further details. This attribute is returned by all end points.
|
||||
*
|
||||
* @property boolean $isSatelliteProvider This is true if the IP belongs
|
||||
* to a satellite Internet provider. This attribute is returned by all
|
||||
* end points.
|
||||
*
|
||||
* @property string $isp The name of the ISP associated with the IP address.
|
||||
* This attribute is only available from the City/ISP/Org and Omni end
|
||||
* points.
|
||||
*
|
||||
* @property string $organization The name of the organization associated
|
||||
* with the IP address. This attribute is only available from the City/ISP/Org
|
||||
* and Omni end points.
|
||||
*
|
||||
* @property string $userType <p>The user type associated with the IP
|
||||
* address. This can be one of the following values:</p>
|
||||
* <ul>
|
||||
* <li>business
|
||||
* <li>cafe
|
||||
* <li>cellular
|
||||
* <li>college
|
||||
* <li>content_delivery_network
|
||||
* <li>dialup
|
||||
* <li>government
|
||||
* <li>hosting
|
||||
* <li>library
|
||||
* <li>military
|
||||
* <li>residential
|
||||
* <li>router
|
||||
* <li>school
|
||||
* <li>search_engine_spider
|
||||
* <li>traveler
|
||||
* </ul>
|
||||
* <p>This attribute is only available from the Omni end point.</p>
|
||||
*/
|
||||
class Traits extends AbstractRecord
|
||||
{
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
protected $validAttributes = array(
|
||||
'autonomousSystemNumber',
|
||||
'autonomousSystemOrganization',
|
||||
'domain',
|
||||
'isAnonymousProxy',
|
||||
'isSatelliteProvider',
|
||||
'isp',
|
||||
'ipAddress',
|
||||
'organization',
|
||||
'userType'
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\WebService;
|
||||
|
||||
use GeoIp2\Exception\GeoIp2Exception;
|
||||
use GeoIp2\Exception\HttpException;
|
||||
use GeoIp2\Exception\WebServiceException;
|
||||
use GeoIp2\Model\City;
|
||||
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\Http\Exception\ClientErrorResponseException;
|
||||
use Guzzle\Http\Exception\ServerErrorResponseException;
|
||||
|
||||
/**
|
||||
* This class provides a client API for all the GeoIp2 web service's
|
||||
* end points. The end points are Country, City, City/ISP/Org, and Omni. Each
|
||||
* end point returns a different set of data about an IP address, with Country
|
||||
* returning the least data and Omni the most.
|
||||
*
|
||||
* Each web service end point is represented by a different model class, and
|
||||
* these model classes in turn contain multiple Record classes. The record
|
||||
* classes have attributes which contain data about the IP address.
|
||||
*
|
||||
* If the web service does not return a particular piece of data for an IP
|
||||
* address, the associated attribute is not populated.
|
||||
*
|
||||
* The web service may not return any information for an entire record, in
|
||||
* which case all of the attributes for that record class will be empty.
|
||||
*
|
||||
* **Usage**
|
||||
*
|
||||
* The basic API for this class is the same for all of the web service end
|
||||
* points. First you create a web service object with your MaxMind
|
||||
* <code>$userId</code> and <code>$licenseKey</code>, 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.
|
||||
*
|
||||
* If the request fails, the client class throws an exception.
|
||||
*
|
||||
* **Exceptions**
|
||||
*
|
||||
* For details on the possible errors returned by the web service itself, see
|
||||
* {@link http://dev.maxmind.com/geoip2/geoip/web-services the GeoIP2 web
|
||||
* service docs}.
|
||||
*
|
||||
* If the web service returns an explicit error document, this is thrown as a
|
||||
* {@link \GeoIp2\Exception\WebServiceException}. If some other sort of
|
||||
* transport error occurs, this is thrown as a {@link
|
||||
* \GeoIp2\Exception\HttpException}. The difference is that the web service
|
||||
* error includes an error message and 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 becomes a {@link \GeoIp2\Exception\HttpException}.
|
||||
*
|
||||
* Finally, if the web service returns a 200 but the body is invalid, the
|
||||
* client throws a {@link \GeoIp2\Exception\GeoIp2Exception}.
|
||||
*/
|
||||
class Client
|
||||
{
|
||||
private $userId;
|
||||
private $licenseKey;
|
||||
private $languages;
|
||||
private $host;
|
||||
private $guzzleClient;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $userId Your MaxMind user ID
|
||||
* @param string $licenseKey Your MaxMind license key
|
||||
* @param array $languages List of language codes to use in name property
|
||||
* from most preferred to least preferred.
|
||||
* @param string $host Optional host parameter
|
||||
* @param object $guzzleClient Optional Guzzle client to use (to facilitate
|
||||
* unit testing).
|
||||
*/
|
||||
public function __construct(
|
||||
$userId,
|
||||
$licenseKey,
|
||||
$languages = array('en'),
|
||||
$host = 'geoip.maxmind.com',
|
||||
$guzzleClient = null
|
||||
) {
|
||||
$this->userId = $userId;
|
||||
$this->licenseKey = $licenseKey;
|
||||
$this->languages = $languages;
|
||||
$this->host = $host;
|
||||
// To enable unit testing
|
||||
$this->guzzleClient = $guzzleClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calls the GeoIP2 City endpoint.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||
* address is provided, the address that the web service is called
|
||||
* from will be used.
|
||||
*
|
||||
* @return \GeoIp2\Model\City
|
||||
*
|
||||
* @throws \GeoIp2\Exception\GeoIp2Exception if there was a generic
|
||||
* error processing your request.
|
||||
* @throws \GeoIp2\Exception\HttpException if there was an HTTP transport
|
||||
* error.
|
||||
* @throws \GeoIp2\Exception\WebServiceException if an error was returned
|
||||
* by MaxMind's GeoIp2 web service.
|
||||
*/
|
||||
public function city($ipAddress = 'me')
|
||||
{
|
||||
return $this->responseFor('city', 'City', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calls the GeoIP2 Country endpoint.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||
* address is provided, the address that the web service is called
|
||||
* from will be used.
|
||||
*
|
||||
* @return \GeoIp2\Model\Country
|
||||
*
|
||||
* @throws \GeoIp2\Exception\GeoIp2Exception if there was a generic
|
||||
* error processing your request.
|
||||
* @throws \GeoIp2\Exception\HttpException if there was an HTTP transport
|
||||
* error.
|
||||
* @throws \GeoIp2\Exception\WebServiceException if an error was returned
|
||||
* by MaxMind's GeoIp2 web service.
|
||||
*/
|
||||
public function country($ipAddress = 'me')
|
||||
{
|
||||
return $this->responseFor('country', 'Country', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calls the GeoIP2 City/ISP/Org endpoint.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||
* address is provided, the address that the web service is called
|
||||
* from will be used.
|
||||
*
|
||||
* @return \GeoIp2\Model\CityIspOrg
|
||||
*
|
||||
* @throws \GeoIp2\Exception\GeoIp2Exception if there was a generic
|
||||
* error processing your request.
|
||||
* @throws \GeoIp2\Exception\HttpException if there was an HTTP transport
|
||||
* error.
|
||||
* @throws \GeoIp2\Exception\WebServiceException if an error was returned
|
||||
* by MaxMind's GeoIp2 web service.
|
||||
*/
|
||||
public function cityIspOrg($ipAddress = 'me')
|
||||
{
|
||||
return $this->responseFor('city_isp_org', 'CityIspOrg', $ipAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method calls the GeoIP2 Omni endpoint.
|
||||
*
|
||||
* @param string $ipAddress IPv4 or IPv6 address as a string. If no
|
||||
* address is provided, the address that the web service is called
|
||||
* from will be used.
|
||||
*
|
||||
* @return \GeoIp2\Model\Omni
|
||||
*
|
||||
* @throws \GeoIp2\Exception\GeoIp2Exception if there was a generic
|
||||
* error processing your request.
|
||||
* @throws \GeoIp2\Exception\HttpException if there was an HTTP transport
|
||||
* error.
|
||||
* @throws \GeoIp2\Exception\WebServiceException if an error was returned
|
||||
* by MaxMind's GeoIp2 web service.
|
||||
*/
|
||||
public function omni($ipAddress = 'me')
|
||||
{
|
||||
return $this->responseFor('omni', 'Omni', $ipAddress);
|
||||
}
|
||||
|
||||
private function responseFor($endpoint, $class, $ipAddress)
|
||||
{
|
||||
$uri = implode('/', array($this->baseUri(), $endpoint, $ipAddress));
|
||||
|
||||
$client = $this->guzzleClient ?
|
||||
$this->guzzleClient : new GuzzleClient();
|
||||
$request = $client->get($uri, array('Accept' => 'application/json'));
|
||||
$request->setAuth($this->userId, $this->licenseKey);
|
||||
$ua = $request->getHeader('User-Agent');
|
||||
$ua = "GeoIP2 PHP API ($ua)";
|
||||
$request->setHeader('User-Agent', $ua);
|
||||
|
||||
$response = null;
|
||||
try {
|
||||
$response = $request->send();
|
||||
} catch (ClientErrorResponseException $e) {
|
||||
$this->handle4xx($e->getResponse(), $uri);
|
||||
} catch (ServerErrorResponseException $e) {
|
||||
$this->handle5xx($e->getResponse(), $uri);
|
||||
}
|
||||
|
||||
if ($response && $response->isSuccessful()) {
|
||||
$body = $this->handleSuccess($response, $uri);
|
||||
$class = "GeoIp2\\Model\\" . $class;
|
||||
return new $class($body, $this->languages);
|
||||
} else {
|
||||
$this->handleNon200($response, $uri);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleSuccess($response, $uri)
|
||||
{
|
||||
if ($response->getContentLength() == 0) {
|
||||
throw new GeoIp2Exception(
|
||||
"Received a 200 response for $uri but did not " .
|
||||
"receive a HTTP body."
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return $response->json();
|
||||
} catch (RuntimeException $e) {
|
||||
throw new GeoIp2Exception(
|
||||
"Received a 200 response for $uri but could not decode " .
|
||||
"the response as JSON: " . $e->getMessage()
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function handle4xx($response, $uri)
|
||||
{
|
||||
$status = $response->getStatusCode();
|
||||
|
||||
$body = array();
|
||||
|
||||
if ($response->getContentLength() > 0) {
|
||||
if (strstr($response->getContentType(), 'json')) {
|
||||
try {
|
||||
$body = $response->json();
|
||||
if (!isset($body['code']) || !isset($body['error'])) {
|
||||
throw new GeoIp2Exception(
|
||||
'Response contains JSON but it does not specify ' .
|
||||
'code or error keys: ' . $response->getBody()
|
||||
);
|
||||
}
|
||||
} catch (RuntimeException $e) {
|
||||
throw new HttpException(
|
||||
"Received a $status error for $uri but it did not " .
|
||||
"include the expected JSON body: " .
|
||||
$e->getMessage(),
|
||||
$status,
|
||||
$uri
|
||||
);
|
||||
}
|
||||
} else {
|
||||
throw new HttpException(
|
||||
"Received a $status error for $uri with the " .
|
||||
"following body: " . $response->getBody(),
|
||||
$status,
|
||||
$uri
|
||||
);
|
||||
}
|
||||
} else {
|
||||
throw new HttpException(
|
||||
"Received a $status error for $uri with no body",
|
||||
$status,
|
||||
$uri
|
||||
);
|
||||
}
|
||||
|
||||
throw new WebServiceException(
|
||||
$body['error'],
|
||||
$body['code'],
|
||||
$status,
|
||||
$uri
|
||||
);
|
||||
}
|
||||
|
||||
private function handle5xx($response, $uri)
|
||||
{
|
||||
$status = $response->getStatusCode();
|
||||
|
||||
throw new HttpException(
|
||||
"Received a server error ($status) for $uri",
|
||||
$status,
|
||||
$uri
|
||||
);
|
||||
}
|
||||
|
||||
private function handleNon200($response, $uri)
|
||||
{
|
||||
$status = $response->getStatusCode();
|
||||
|
||||
throw new HttpException(
|
||||
"Received a very surprising HTTP status " .
|
||||
"($status) for $uri",
|
||||
$status,
|
||||
$uri
|
||||
);
|
||||
}
|
||||
|
||||
private function baseUri()
|
||||
{
|
||||
return 'https://' . $this->host . '/geoip/v2.0';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user