1: <?php
2:
3: namespace GeoIp2\WebService;
4:
5: use GeoIp2\Exception\AddressNotFoundException;
6: use GeoIp2\Exception\AuthenticationException;
7: use GeoIp2\Exception\GeoIp2Exception;
8: use GeoIp2\Exception\HttpException;
9: use GeoIp2\Exception\InvalidRequestException;
10: use GeoIp2\Exception\OutOfQueriesException;
11: use GeoIp2\ProviderInterface;
12: use Guzzle\Common\Exception\RuntimeException;
13: use Guzzle\Http\Client as GuzzleClient;
14: use Guzzle\Http\Exception\ClientErrorResponseException;
15: use Guzzle\Http\Exception\ServerErrorResponseException;
16:
17: /**
18: * This class provides a client API for all the GeoIP2 Precision web service
19: * end points. The end points are Country, City, and Insights. Each end point
20: * returns a different set of data about an IP address, with Country returning
21: * the least data and Insights the most.
22: *
23: * Each web service end point is represented by a different model class, and
24: * these model classes in turn contain multiple Record classes. The record
25: * classes have attributes which contain data about the IP address.
26: *
27: * If the web service does not return a particular piece of data for an IP
28: * address, the associated attribute is not populated.
29: *
30: * The web service may not return any information for an entire record, in
31: * which case all of the attributes for that record class will be empty.
32: *
33: * **Usage**
34: *
35: * The basic API for this class is the same for all of the web service end
36: * points. First you create a web service object with your MaxMind
37: * <code>$userId</code> and <code>$licenseKey</code>, then you call the method
38: * corresponding to a specific end point, passing it the IP address you want
39: * to look up.
40: *
41: * If the request succeeds, the method call will return a model class for
42: * the end point you called. This model in turn contains multiple record
43: * classes, each of which represents part of the data returned by the web
44: * service.
45: *
46: * If the request fails, the client class throws an exception.
47: */
48: class Client implements ProviderInterface
49: {
50: private $userId;
51: private $licenseKey;
52: private $locales;
53: private $host;
54: private $guzzleClient;
55:
56: /**
57: * Constructor.
58: *
59: * @param int $userId Your MaxMind user ID
60: * @param string $licenseKey Your MaxMind license key
61: * @param array $locales List of locale codes to use in name property
62: * from most preferred to least preferred.
63: * @param string $host Optional host parameter
64: * @param object $guzzleClient Optional Guzzle client to use (to facilitate
65: * unit testing).
66: */
67: public function __construct(
68: $userId,
69: $licenseKey,
70: $locales = array('en'),
71: $host = 'geoip.maxmind.com',
72: $guzzleClient = null
73: ) {
74: $this->userId = $userId;
75: $this->licenseKey = $licenseKey;
76: $this->locales = $locales;
77: $this->host = $host;
78: // To enable unit testing
79: $this->guzzleClient = $guzzleClient;
80: }
81:
82: /**
83: * This method calls the GeoIP2 Precision: City endpoint.
84: *
85: * @param string $ipAddress IPv4 or IPv6 address as a string. If no
86: * address is provided, the address that the web service is called
87: * from will be used.
88: *
89: * @return \GeoIp2\Model\City
90: *
91: * @throws \GeoIp2\Exception\AddressNotFoundException if the address you
92: * provided is not in our database (e.g., a private address).
93: * @throws \GeoIp2\Exception\AuthenticationException if there is a problem
94: * with the user ID or license key that you provided.
95: * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
96: * of queries.
97: * @throws \GeoIp2\Exception\InvalidRequestException} if your request was
98: * received by the web service but is invalid for some other reason.
99: * This may indicate an issue with this API. Please report the error to
100: * MaxMind.
101: * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
102: * code or message was returned. This could indicate a problem with the
103: * connection between your server and the web service or that the web
104: * service returned an invalid document or 500 error code.
105: * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
106: * class to the above exceptions. It will be thrown directly if a 200
107: * status code is returned but the body is invalid.
108: */
109: public function city($ipAddress = 'me')
110: {
111: return $this->responseFor('city', 'City', $ipAddress);
112: }
113:
114: /**
115: * This method calls the GeoIP2 Precision: Country endpoint.
116: *
117: * @param string $ipAddress IPv4 or IPv6 address as a string. If no
118: * address is provided, the address that the web service is called
119: * from will be used.
120: *
121: * @return \GeoIp2\Model\Country
122: *
123: * @throws \GeoIp2\Exception\AddressNotFoundException if the address you
124: * provided is not in our database (e.g., a private address).
125: * @throws \GeoIp2\Exception\AuthenticationException if there is a problem
126: * with the user ID or license key that you provided.
127: * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
128: * of queries.
129: * @throws \GeoIp2\Exception\InvalidRequestException} if your request was
130: * received by the web service but is invalid for some other reason.
131: * This may indicate an issue with this API. Please report the error to
132: * MaxMind.
133: * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
134: * code or message was returned. This could indicate a problem with the
135: * connection between your server and the web service or that the web
136: * service returned an invalid document or 500 error code.
137: * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
138: * class to the above exceptions. It will be thrown directly if a 200
139: * status code is returned but the body is invalid.
140: */
141: public function country($ipAddress = 'me')
142: {
143: return $this->responseFor('country', 'Country', $ipAddress);
144: }
145:
146: /**
147: * This method calls the GeoIP2 Precision: Insights endpoint.
148: *
149: * @param string $ipAddress IPv4 or IPv6 address as a string. If no
150: * address is provided, the address that the web service is called
151: * from will be used.
152: *
153: * @return \GeoIp2\Model\Insights
154: *
155: * @throws \GeoIp2\Exception\AddressNotFoundException if the address you
156: * provided is not in our database (e.g., a private address).
157: * @throws \GeoIp2\Exception\AuthenticationException if there is a problem
158: * with the user ID or license key that you provided.
159: * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
160: * of queries.
161: * @throws \GeoIp2\Exception\InvalidRequestException} if your request was
162: * received by the web service but is invalid for some other reason.
163: * This may indicate an issue with this API. Please report the error to
164: * MaxMind.
165: * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
166: * code or message was returned. This could indicate a problem with the
167: * connection between your server and the web service or that the web
168: * service returned an invalid document or 500 error code.
169: * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
170: * class to the above exceptions. It will be thrown directly if a 200
171: * status code is returned but the body is invalid.
172: */
173: public function insights($ipAddress = 'me')
174: {
175: return $this->responseFor('insights', 'Insights', $ipAddress);
176: }
177:
178: private function responseFor($endpoint, $class, $ipAddress)
179: {
180: $uri = implode('/', array($this->baseUri(), $endpoint, $ipAddress));
181:
182: $client = $this->guzzleClient ?
183: $this->guzzleClient : new GuzzleClient();
184: $request = $client->get($uri, array('Accept' => 'application/json'));
185: $request->setAuth($this->userId, $this->licenseKey);
186: $this->setUserAgent($request);
187:
188: try {
189: $response = $request->send();
190: } catch (ClientErrorResponseException $e) {
191: $this->handle4xx($e->getResponse(), $uri);
192: } catch (ServerErrorResponseException $e) {
193: $this->handle5xx($e->getResponse(), $uri);
194: }
195:
196: if ($response && $response->isSuccessful()) {
197: $body = $this->handleSuccess($response, $uri);
198: $class = "GeoIp2\\Model\\" . $class;
199: return new $class($body, $this->locales);
200: } else {
201: $this->handleNon200($response, $uri);
202: }
203: }
204:
205: private function handleSuccess($response, $uri)
206: {
207: if ($response->getContentLength() == 0) {
208: throw new GeoIp2Exception(
209: "Received a 200 response for $uri but did not " .
210: "receive a HTTP body."
211: );
212: }
213:
214: try {
215: return $response->json();
216: } catch (RuntimeException $e) {
217: throw new GeoIp2Exception(
218: "Received a 200 response for $uri but could not decode " .
219: "the response as JSON: " . $e->getMessage()
220: );
221:
222: }
223: }
224:
225: private function handle4xx($response, $uri)
226: {
227: $status = $response->getStatusCode();
228:
229: if ($response->getContentLength() > 0) {
230: if (strstr($response->getContentType(), 'json')) {
231: try {
232: $body = $response->json();
233: if (!isset($body['code']) || !isset($body['error'])) {
234: throw new GeoIp2Exception(
235: 'Response contains JSON but it does not specify ' .
236: 'code or error keys: ' . $response->getBody()
237: );
238: }
239: } catch (RuntimeException $e) {
240: throw new HttpException(
241: "Received a $status error for $uri but it did not " .
242: "include the expected JSON body: " .
243: $e->getMessage(),
244: $status,
245: $uri
246: );
247: }
248: } else {
249: throw new HttpException(
250: "Received a $status error for $uri with the " .
251: "following body: " . $response->getBody(),
252: $status,
253: $uri
254: );
255: }
256: } else {
257: throw new HttpException(
258: "Received a $status error for $uri with no body",
259: $status,
260: $uri
261: );
262: }
263: $this->handleWebServiceError(
264: $body['error'],
265: $body['code'],
266: $status,
267: $uri
268: );
269: }
270:
271: private function handleWebServiceError($message, $code, $status, $uri)
272: {
273: switch ($code) {
274: case 'IP_ADDRESS_NOT_FOUND':
275: case 'IP_ADDRESS_RESERVED':
276: throw new AddressNotFoundException($message);
277: case 'AUTHORIZATION_INVALID':
278: case 'LICENSE_KEY_REQUIRED':
279: case 'USER_ID_REQUIRED':
280: throw new AuthenticationException($message);
281: case 'OUT_OF_QUERIES':
282: throw new OutOfQueriesException($message);
283: default:
284: throw new InvalidRequestException(
285: $message,
286: $code,
287: $status,
288: $uri
289: );
290: }
291: }
292:
293: private function handle5xx($response, $uri)
294: {
295: $status = $response->getStatusCode();
296:
297: throw new HttpException(
298: "Received a server error ($status) for $uri",
299: $status,
300: $uri
301: );
302: }
303:
304: private function handleNon200($response, $uri)
305: {
306: $status = $response->getStatusCode();
307:
308: throw new HttpException(
309: "Received a very surprising HTTP status " .
310: "($status) for $uri",
311: $status,
312: $uri
313: );
314: }
315:
316: private function setUserAgent($request)
317: {
318: $userAgent = $request->getHeader('User-Agent');
319: $userAgent = "GeoIP2 PHP API ($userAgent)";
320: $request->setHeader('User-Agent', $userAgent);
321: }
322:
323: private function baseUri()
324: {
325: return 'https://' . $this->host . '/geoip/v2.1';
326: }
327: }
328: