2013-05-07 17:02:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace GeoIP2\Webservice;
|
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
use GeoIP2\Exception\GenericException;
|
2013-05-07 20:13:11 +00:00
|
|
|
use GeoIP2\Exception\HttpException;
|
2013-05-08 21:42:13 +00:00
|
|
|
use GeoIP2\Exception\WebserviceException;
|
2013-05-07 17:02:39 +00:00
|
|
|
use GeoIP2\Model\City;
|
2013-05-08 21:42:13 +00:00
|
|
|
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;
|
2013-05-08 21:42:13 +00:00
|
|
|
use Guzzle\Common\Exception\RuntimeException;
|
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException;
|
|
|
|
use Guzzle\Http\Exception\ServerErrorResponseException;
|
2013-05-07 17:06:57 +00:00
|
|
|
|
|
|
|
class Client
|
|
|
|
{
|
|
|
|
|
2013-05-08 15:16:18 +00:00
|
|
|
private $userId;
|
|
|
|
private $licenseKey;
|
2013-05-07 22:02:53 +00:00
|
|
|
private $languages;
|
2013-05-08 15:16:18 +00:00
|
|
|
private $baseUri = 'https://geoip.maxmind.com/geoip/v2.0';
|
2013-05-08 21:42:13 +00:00
|
|
|
private $guzzleClient;
|
2013-05-07 17:39:06 +00:00
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
public function __construct($userId, $licenseKey, $languages=array('en'),
|
|
|
|
$guzzleClient = null)
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 15:16:18 +00:00
|
|
|
$this->userId = $userId;
|
|
|
|
$this->licenseKey = $licenseKey;
|
2013-05-07 22:02:53 +00:00
|
|
|
$this->languages = $languages;
|
2013-05-08 21:42:13 +00:00
|
|
|
// To enable unit testing
|
|
|
|
$this->guzzleClient = $guzzleClient;
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 15:16:18 +00:00
|
|
|
public function city($ipAddress = 'me')
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 15:16:18 +00:00
|
|
|
return $this->responseFor('city', 'City', $ipAddress);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 15:16:18 +00:00
|
|
|
public function country($ipAddress = 'me')
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 15:16:18 +00:00
|
|
|
return $this->responseFor('country', 'Country', $ipAddress);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
public function cityIspOrg($ipAddress = 'me')
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 21:42:13 +00:00
|
|
|
return $this->responseFor('city_isp_org', 'CityIspOrg', $ipAddress);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 15:16:18 +00:00
|
|
|
public function omni($ipAddress = 'me')
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 15:16:18 +00:00
|
|
|
return $this->responseFor('omni', 'Omni', $ipAddress);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 15:16:18 +00:00
|
|
|
private function responseFor($path, $class, $ipAddress)
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 15:16:18 +00:00
|
|
|
$uri = implode('/', array($this->baseUri, $path, $ipAddress));
|
2013-05-07 20:13:11 +00:00
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
$client = $this->guzzleClient ? $this->guzzleClient : new GuzzleClient();
|
2013-05-07 18:17:38 +00:00
|
|
|
$request = $client->get($uri, array('Accept' => 'application/json'));
|
2013-05-08 15:16:18 +00:00
|
|
|
$request->setAuth($this->userId, $this->licenseKey);
|
2013-05-07 20:13:11 +00:00
|
|
|
$ua = $request->getHeader('User-Agent');
|
|
|
|
$ua = "GeoIP2 PHP API ($ua)";
|
|
|
|
$request->setHeader('User-Agent', $ua);
|
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
$response = null;
|
|
|
|
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
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
if ($response && $response->isSuccessful()) {
|
2013-05-07 20:13:11 +00:00
|
|
|
$body = $this->handleSuccess($response, $uri);
|
|
|
|
$class = "GeoIP2\\Model\\" . $class;
|
2013-05-07 22:02:53 +00:00
|
|
|
return new $class($body, $this->languages);
|
2013-05-07 20:13:11 +00:00
|
|
|
}
|
2013-05-08 21:42:13 +00:00
|
|
|
else {
|
|
|
|
$this->handleNon200($response, $uri);
|
|
|
|
}
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
private function handleSuccess($response, $uri)
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 21:42:13 +00:00
|
|
|
if ($response->getContentLength() == 0) {
|
|
|
|
throw new GenericException("Received a 200 response for $uri but did not receive a HTTP body.");
|
|
|
|
}
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
try {
|
|
|
|
return $response->json();
|
|
|
|
}
|
2013-05-08 21:42:13 +00:00
|
|
|
catch (RuntimeException $e) {
|
2013-05-07 20:13:11 +00:00
|
|
|
throw new GenericException("Received a 200 response for $uri but could not decode the response as JSON: " . $e->getMessage());
|
|
|
|
|
|
|
|
}
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
private function handle4xx($response, $uri)
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-07 20:13:11 +00:00
|
|
|
$status = $response->getStatusCode();
|
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
$body = array();
|
2013-05-07 17:39:06 +00:00
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
if ( $response->getContentLength() > 0 ) {
|
|
|
|
if( strstr($response->getContentType(), 'json')) {
|
|
|
|
try {
|
|
|
|
$body = $response->json();
|
2013-05-08 21:42:13 +00:00
|
|
|
if (!isset($body['code']) || !isset($body['error']) ){
|
|
|
|
throw new GenericException('Response contains JSON but it does not specify code or error keys: ' . $response->getBody());
|
2013-05-07 20:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-08 21:42:13 +00:00
|
|
|
catch (RuntimeException $e){
|
2013-05-07 20:13:11 +00:00
|
|
|
throw new HttpException("Received a $status error for $uri but it did not include the expected JSON body: " . $e->getMessage(), $status, $uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-05-08 21:42:13 +00:00
|
|
|
throw new HttpException("Received a $status error for $uri with the following body: " . $response->getBody(),
|
2013-05-07 20:13:11 +00:00
|
|
|
$status, $uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new HttpException("Received a $status error for $uri with no body",
|
|
|
|
$status, $uri);
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:42:13 +00:00
|
|
|
throw new WebserviceException($body['error'], $body['code'], $status, $uri);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
private function handle5xx($response, $uri)
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-08 21:42:13 +00:00
|
|
|
$status = $response->getStatusCode();
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
throw new HttpException("Received a server error ($status) for $uri",
|
|
|
|
$status,$uri);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
private function handleNon200($response, $uri)
|
|
|
|
{
|
2013-05-08 21:42:13 +00:00
|
|
|
$status = $response->getStatusCode();
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
throw new HttpException("Received a very surprising HTTP status " .
|
|
|
|
"($status) for $uri",
|
|
|
|
$status, $uri);
|
|
|
|
}
|
2013-05-07 21:23:19 +00:00
|
|
|
}
|