2013-05-07 17:02:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace GeoIP2\Webservice;
|
|
|
|
|
|
|
|
use GeoIP2\Error\Generic;
|
2013-05-07 20:13:11 +00:00
|
|
|
use GeoIP2\Exception\HttpException;
|
2013-05-07 17:02:39 +00:00
|
|
|
use GeoIP2\Error\Webservice;
|
|
|
|
use GeoIP2\Model\City;
|
|
|
|
use GeoIP2\Model\CityISPOrg;
|
|
|
|
use GeoIP2\Model\Country;
|
|
|
|
use GeoIP2\Model\Omni;
|
2013-05-07 18:17:38 +00:00
|
|
|
use Guzzle\Http\Client as GuzzleClient;
|
2013-05-07 17:06:57 +00:00
|
|
|
|
|
|
|
class Client
|
|
|
|
{
|
|
|
|
|
2013-05-07 17:39:06 +00:00
|
|
|
private $user_id;
|
|
|
|
private $license_key;
|
2013-05-07 22:02:53 +00:00
|
|
|
private $languages;
|
2013-05-07 18:17:38 +00:00
|
|
|
private $base_uri = 'https://geoip.maxmind.com/geoip/v2.0';
|
2013-05-07 17:39:06 +00:00
|
|
|
|
2013-05-07 22:02:53 +00:00
|
|
|
public function __construct($user_id, $license_key, $languages=array('en'))
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
|
|
|
$this->user_id = $user_id;
|
|
|
|
$this->license_key = $license_key;
|
2013-05-07 22:02:53 +00:00
|
|
|
$this->languages = $languages;
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function city($ip_address = 'me')
|
|
|
|
{
|
2013-05-07 20:13:11 +00:00
|
|
|
return $this->response_for('city', 'City', $ip_address);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function country($ip_address = 'me')
|
|
|
|
{
|
2013-05-07 20:13:11 +00:00
|
|
|
return $this->response_for('country', 'Country', $ip_address);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cityISPOrg($ip_address = 'me')
|
|
|
|
{
|
2013-05-07 20:13:11 +00:00
|
|
|
return $this->response_for('city_isp_org', 'CityISPOrg', $ip_address);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function omni($ip_address = 'me')
|
|
|
|
{
|
2013-05-07 20:13:11 +00:00
|
|
|
return $this->response_for('omni', 'Omni', $ip_address);
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
private function response_for($path, $class, $ip_address)
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-07 18:17:38 +00:00
|
|
|
$uri = implode('/', array($this->base_uri, $path, $ip_address));
|
2013-05-07 20:13:11 +00:00
|
|
|
|
2013-05-07 18:17:38 +00:00
|
|
|
$client = new GuzzleClient();
|
|
|
|
$request = $client->get($uri, array('Accept' => 'application/json'));
|
|
|
|
$request->setAuth($this->user_id, $this->license_key);
|
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-07 18:17:38 +00:00
|
|
|
$response = $request->send();
|
2013-05-07 20:13:11 +00:00
|
|
|
|
|
|
|
if ($response->isSuccessful()) {
|
|
|
|
$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-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-07 20:13:11 +00:00
|
|
|
// XXX - handle exceptions
|
|
|
|
try {
|
|
|
|
return $response->json();
|
|
|
|
}
|
|
|
|
// XXX - figure out what sort of exception to catch
|
|
|
|
catch (Exception $e) {
|
|
|
|
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-07 20:13:11 +00:00
|
|
|
private function handleError($response, $uri)
|
2013-05-07 17:39:06 +00:00
|
|
|
{
|
2013-05-07 20:13:11 +00:00
|
|
|
$status = $response->getStatusCode();
|
|
|
|
|
|
|
|
if ($status >= 400 && $status <= 499) {
|
|
|
|
$this->handle4xx($response, $uri);
|
|
|
|
}
|
|
|
|
elseif ($status >= 500 && $status <= 599){
|
|
|
|
$this->handle5xx($response, $uri);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->hanldeNon200($reponse, $uri);
|
|
|
|
}
|
2013-05-07 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 20:13:11 +00:00
|
|
|
private function handle4xx($response, $uri)
|
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();
|
|
|
|
if (!$body['code'] || $body['error'] ){
|
|
|
|
throw new GenericException('Response contains JSON but it does not specify code or error keys');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// XXX - don't catch all exceptions
|
|
|
|
catch (Exception $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: $content",
|
|
|
|
$status, $uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new HttpException("Received a $status error for $uri with no body",
|
|
|
|
$status, $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new WebserviceException($body['error'], $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-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)
|
|
|
|
{
|
|
|
|
throw new HttpException("Received a very surprising HTTP status " .
|
|
|
|
"($status) for $uri",
|
|
|
|
$status, $uri);
|
|
|
|
}
|
2013-05-07 21:23:19 +00:00
|
|
|
}
|