locales = $locales; // This is for backwards compatibility. Do not remove except for a // major version bump. if (is_string($options)) { $options = ['host' => $options]; } if (!isset($options['host'])) { $options['host'] = 'geoip.maxmind.com'; } $options['userAgent'] = $this->userAgent(); $this->client = new WsClient($accountId, $licenseKey, $options); } private function userAgent() { return 'GeoIP2-API/' . self::VERSION; } /** * This method calls the GeoIP2 Precision: City service. * * @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. * * @throws \GeoIp2\Exception\AddressNotFoundException if the address you * provided is not in our database (e.g., a private address). * @throws \GeoIp2\Exception\AuthenticationException if there is a problem * with the account ID or license key that you provided * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out * of queries * @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is * invalid for some other reason. This may indicate an issue * with this API. Please report the error to MaxMind. * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error code or message was returned. * This could indicate a problem with the connection between * your server and the web service or that the web service * returned an invalid document or 500 error code. * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent * class to the above exceptions. It will be thrown directly * if a 200 status code is returned but the body is invalid. * * @return \GeoIp2\Model\City */ public function city($ipAddress = 'me') { return $this->responseFor('city', 'City', $ipAddress); } /** * This method calls the GeoIP2 Precision: Country service. * * @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. * * @throws \GeoIp2\Exception\AddressNotFoundException if the address you provided is not in our database (e.g., * a private address). * @throws \GeoIp2\Exception\AuthenticationException if there is a problem * with the account ID or license key that you provided * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out of queries * @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is * invalid for some other reason. This may indicate an * issue with this API. Please report the error to MaxMind. * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error * code or message was returned. This could indicate a problem * with the connection between your server and the web service * or that the web service returned an invalid document or 500 * error code. * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent class to the above exceptions. It * will be thrown directly if a 200 status code is returned but * the body is invalid. * * @return \GeoIp2\Model\Country */ public function country($ipAddress = 'me') { return $this->responseFor('country', 'Country', $ipAddress); } /** * This method calls the GeoIP2 Precision: Insights service. * * @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. * * @throws \GeoIp2\Exception\AddressNotFoundException if the address you * provided is not in our database (e.g., a private address). * @throws \GeoIp2\Exception\AuthenticationException if there is a problem * with the account ID or license key that you provided * @throws \GeoIp2\Exception\OutOfQueriesException if your account is out * of queries * @throws \GeoIp2\Exception\InvalidRequestException} if your request was received by the web service but is * invalid for some other reason. This may indicate an * issue with this API. Please report the error to MaxMind. * @throws \GeoIp2\Exception\HttpException if an unexpected HTTP error code or message was returned. * This could indicate a problem with the connection between * your server and the web service or that the web service * returned an invalid document or 500 error code. * @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent * class to the above exceptions. It will be thrown directly * if a 200 status code is returned but the body is invalid. * * @return \GeoIp2\Model\Insights */ public function insights($ipAddress = 'me') { return $this->responseFor('insights', 'Insights', $ipAddress); } private function responseFor($endpoint, $class, $ipAddress) { $path = implode('/', [self::$basePath, $endpoint, $ipAddress]); try { $body = $this->client->get('GeoIP2 ' . $class, $path); } catch (\MaxMind\Exception\IpAddressNotFoundException $ex) { throw new AddressNotFoundException( $ex->getMessage(), $ex->getStatusCode(), $ex ); } catch (\MaxMind\Exception\AuthenticationException $ex) { throw new AuthenticationException( $ex->getMessage(), $ex->getStatusCode(), $ex ); } catch (\MaxMind\Exception\InsufficientFundsException $ex) { throw new OutOfQueriesException( $ex->getMessage(), $ex->getStatusCode(), $ex ); } catch (\MaxMind\Exception\InvalidRequestException $ex) { throw new InvalidRequestException( $ex->getMessage(), $ex->getErrorCode(), $ex->getStatusCode(), $ex->getUri(), $ex ); } catch (\MaxMind\Exception\HttpException $ex) { throw new HttpException( $ex->getMessage(), $ex->getStatusCode(), $ex->getUri(), $ex ); } catch (\MaxMind\Exception\WebServiceException $ex) { throw new GeoIp2Exception( $ex->getMessage(), $ex->getCode(), $ex ); } $class = 'GeoIp2\\Model\\' . $class; return new $class($body, $this->locales); } }