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 web service's
19: * end points. The end points are Country, City, City/ISP/Org, and Omni. Each
20: * end point returns a different set of data about an IP address, with Country
21: * returning the least data and Omni 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 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 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 City/ISP/Org 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\CityIspOrg
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 cityIspOrg($ipAddress = 'me')
174: {
175: return $this->responseFor('city_isp_org', 'CityIspOrg', $ipAddress);
176: }
177:
178: /**
179: * This method calls the GeoIP2 Omni endpoint.
180: *
181: * @param string $ipAddress IPv4 or IPv6 address as a string. If no
182: * address is provided, the address that the web service is called
183: * from will be used.
184: *
185: * @return \GeoIp2\Model\Omni
186: *
187: * @throws \GeoIp2\Exception\AddressNotFoundException if the address you
188: * provided is not in our database (e.g., a private address).
189: * @throws \GeoIp2\Exception\AuthenticationException if there is a problem
190: * with the user ID or license key that you provided.
191: * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out
192: * of queries.
193: * @throws \GeoIp2\Exception\InvalidRequestException} if your request was
194: * received by the web service but is invalid for some other reason.
195: * This may indicate an issue with this API. Please report the error to
196: * MaxMind.
197: * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error
198: * code or message was returned. This could indicate a problem with the
199: * connection between your server and the web service or that the web
200: * service returned an invalid document or 500 error code.
201: * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
202: * class to the above exceptions. It will be thrown directly if a 200
203: * status code is returned but the body is invalid.
204: */
205: public function omni($ipAddress = 'me')
206: {
207: return $this->responseFor('omni', 'Omni', $ipAddress);
208: }
209:
210: private function responseFor($endpoint, $class, $ipAddress)
211: {
212: $uri = implode('/', array($this->baseUri(), $endpoint, $ipAddress));
213:
214: $client = $this->guzzleClient ?
215: $this->guzzleClient : new GuzzleClient();
216: $request = $client->get($uri, array('Accept' => 'application/json'));
217: $request->setAuth($this->userId, $this->licenseKey);
218: $this->setUserAgent($request);
219:
220: try {
221: $response = $request->send();
222: } catch (ClientErrorResponseException $e) {
223: $this->handle4xx($e->getResponse(), $uri);
224: } catch (ServerErrorResponseException $e) {
225: $this->handle5xx($e->getResponse(), $uri);
226: }
227:
228: if ($response && $response->isSuccessful()) {
229: $body = $this->handleSuccess($response, $uri);
230: $class = "GeoIp2\\Model\\" . $class;
231: return new $class($body, $this->locales);
232: } else {
233: $this->handleNon200($response, $uri);
234: }
235: }
236:
237: private function handleSuccess($response, $uri)
238: {
239: if ($response->getContentLength() == 0) {
240: throw new GeoIp2Exception(
241: "Received a 200 response for $uri but did not " .
242: "receive a HTTP body."
243: );
244: }
245:
246: try {
247: return $response->json();
248: } catch (RuntimeException $e) {
249: throw new GeoIp2Exception(
250: "Received a 200 response for $uri but could not decode " .
251: "the response as JSON: " . $e->getMessage()
252: );
253:
254: }
255: }
256:
257: private function handle4xx($response, $uri)
258: {
259: $status = $response->getStatusCode();
260:
261: if ($response->getContentLength() > 0) {
262: if (strstr($response->getContentType(), 'json')) {
263: try {
264: $body = $response->json();
265: if (!isset($body['code']) || !isset($body['error'])) {
266: throw new GeoIp2Exception(
267: 'Response contains JSON but it does not specify ' .
268: 'code or error keys: ' . $response->getBody()
269: );
270: }
271: } catch (RuntimeException $e) {
272: throw new HttpException(
273: "Received a $status error for $uri but it did not " .
274: "include the expected JSON body: " .
275: $e->getMessage(),
276: $status,
277: $uri
278: );
279: }
280: } else {
281: throw new HttpException(
282: "Received a $status error for $uri with the " .
283: "following body: " . $response->getBody(),
284: $status,
285: $uri
286: );
287: }
288: } else {
289: throw new HttpException(
290: "Received a $status error for $uri with no body",
291: $status,
292: $uri
293: );
294: }
295: $this->handleWebServiceError(
296: $body['error'],
297: $body['code'],
298: $status,
299: $uri
300: );
301: }
302:
303: private function handleWebServiceError($message, $code, $status, $uri)
304: {
305: switch ($code) {
306: case 'IP_ADDRESS_NOT_FOUND':
307: case 'IP_ADDRESS_RESERVED':
308: throw new AddressNotFoundException($message);
309: case 'AUTHORIZATION_INVALID':
310: case 'LICENSE_KEY_REQUIRED':
311: case 'USER_ID_REQUIRED':
312: throw new AuthenticationException($message);
313: case 'OUT_OF_QUERIES':
314: throw new OutOfQueriesException($message);
315: default:
316: throw new InvalidRequestException(
317: $message,
318: $code,
319: $status,
320: $uri
321: );
322: }
323: }
324:
325: private function handle5xx($response, $uri)
326: {
327: $status = $response->getStatusCode();
328:
329: throw new HttpException(
330: "Received a server error ($status) for $uri",
331: $status,
332: $uri
333: );
334: }
335:
336: private function handleNon200($response, $uri)
337: {
338: $status = $response->getStatusCode();
339:
340: throw new HttpException(
341: "Received a very surprising HTTP status " .
342: "($status) for $uri",
343: $status,
344: $uri
345: );
346: }
347:
348: private function setUserAgent($request)
349: {
350: $userAgent = $request->getHeader('User-Agent');
351: $userAgent = "GeoIP2 PHP API ($userAgent)";
352: $request->setHeader('User-Agent', $userAgent);
353: }
354:
355: private function baseUri()
356: {
357: return 'https://' . $this->host . '/geoip/v2.0';
358: }
359: }
360: