GeoIP2-php/src/GeoIP2/Webservice/Client.php

313 lines
11 KiB
PHP
Raw Normal View History

2013-05-07 17:02:39 +00:00
<?php
namespace GeoIP2\Webservice;
use GeoIP2\Exception\GenericException;
2013-05-07 20:13:11 +00:00
use GeoIP2\Exception\HttpException;
use GeoIP2\Exception\WebserviceException;
2013-05-07 17:02:39 +00:00
use GeoIP2\Model\City;
use GeoIP2\Model\CityIspOrg;
2013-05-07 17:02:39 +00:00
use GeoIP2\Model\Country;
use GeoIP2\Model\Omni;
2013-05-07 18:17:38 +00:00
use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Common\Exception\RuntimeException;
use Guzzle\Http\Exception\ClientErrorResponseException;
use Guzzle\Http\Exception\ServerErrorResponseException;
2013-05-07 17:06:57 +00:00
2013-05-10 15:30:40 +00:00
/**
* This class provides a client API for all the GeoIP Precision 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 webservice
* 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\GenericException}.
2013-05-10 15:30:40 +00:00
*/
2013-05-07 17:06:57 +00:00
class Client
{
private $userId;
private $licenseKey;
private $languages;
2013-05-09 22:22:11 +00:00
private $host;
private $guzzleClient;
2013-05-10 15:30:40 +00:00
/**
* 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'),
2013-05-09 22:22:11 +00:00
$host = 'geoip.maxmind.com',
$guzzleClient = null
) {
$this->userId = $userId;
$this->licenseKey = $licenseKey;
$this->languages = $languages;
2013-05-09 22:22:11 +00:00
$this->host = $host;
// To enable unit testing
$this->guzzleClient = $guzzleClient;
}
2013-05-07 20:13:11 +00:00
2013-05-10 15:30:40 +00:00
/**
* This method calls the GeoIP2 Precision 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.
*
2013-05-10 17:47:11 +00:00
* @return \GeoIP2\Model\City
*
* @throws \GeoIP2\Exception\GenericException 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.
2013-05-10 15:30:40 +00:00
*/
public function city($ipAddress = 'me')
{
return $this->responseFor('city', 'City', $ipAddress);
2013-05-07 20:13:11 +00:00
}
2013-05-07 17:39:06 +00:00
2013-05-10 15:30:40 +00:00
/**
* 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.
*
2013-05-10 17:47:11 +00:00
* @return \GeoIP2\Model\Country
*
* @throws \GeoIP2\Exception\GenericException 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.
2013-05-10 15:30:40 +00:00
*/
public function country($ipAddress = 'me')
{
return $this->responseFor('country', 'Country', $ipAddress);
}
2013-05-10 15:30:40 +00:00
/**
* This method calls the GeoIP2 Precision 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.
*
2013-05-10 17:47:11 +00:00
* @return \GeoIP2\Model\CityIspOrg
*
* @throws \GeoIP2\Exception\GenericException 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.
2013-05-10 15:30:40 +00:00
*/
public function cityIspOrg($ipAddress = 'me')
{
return $this->responseFor('city_isp_org', 'CityIspOrg', $ipAddress);
2013-05-07 20:13:11 +00:00
}
2013-05-10 15:30:40 +00:00
/**
* This method calls the GeoIP2 Precision 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.
*
2013-05-10 17:47:11 +00:00
* @return \GeoIP2\Model\Omni
*
* @throws \GeoIP2\Exception\GenericException 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.
2013-05-10 15:30:40 +00:00
*/
public function omni($ipAddress = 'me')
{
return $this->responseFor('omni', 'Omni', $ipAddress);
2013-05-07 20:13:11 +00:00
}
2013-05-07 17:39:06 +00:00
2013-05-09 22:22:11 +00:00
private function responseFor($endpoint, $class, $ipAddress)
{
2013-05-09 22:22:11 +00:00
$uri = implode('/', array($this->baseUri(), $endpoint, $ipAddress));
2013-05-07 20:13:11 +00:00
$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);
2013-05-07 17:39:06 +00:00
$response = null;
2013-05-07 20:13:11 +00:00
try {
$response = $request->send();
} catch (ClientErrorResponseException $e) {
$this->handle4xx($e->getResponse(), $uri);
} catch (ServerErrorResponseException $e) {
$this->handle5xx($e->getResponse(), $uri);
2013-05-07 20:13:11 +00:00
}
if ($response && $response->isSuccessful()) {
$body = $this->handleSuccess($response, $uri);
$class = "GeoIP2\\Model\\" . $class;
return new $class($body, $this->languages);
} else {
$this->handleNon200($response, $uri);
2013-05-07 20:13:11 +00:00
}
}
private function handleSuccess($response, $uri)
{
if ($response->getContentLength() == 0) {
throw new GenericException(
"Received a 200 response for $uri but did not " .
"receive a HTTP body."
);
}
try {
return $response->json();
} catch (RuntimeException $e) {
throw new GenericException(
"Received a 200 response for $uri but could not decode " .
"the response as JSON: " . $e->getMessage()
);
}
2013-05-07 20:13:11 +00:00
}
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 GenericException(
'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
);
}
2013-05-07 17:39:06 +00:00
throw new WebserviceException(
$body['error'],
$body['code'],
$status,
$uri
);
}
private function handle5xx($response, $uri)
{
$status = $response->getStatusCode();
2013-05-07 17:39:06 +00:00
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
);
}
2013-05-09 22:22:11 +00:00
2013-05-09 22:23:37 +00:00
private function baseUri()
{
2013-05-09 22:22:11 +00:00
return 'https://' . $this->host . '/geoip/v2.0';
}
}