PHPackages                             floridaman963/geoip2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. floridaman963/geoip2

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

floridaman963/geoip2
====================

MaxMind GeoIP2 PHP API

09PHP

Since Apr 23Pushed 3y ago1 watchersCompare

[ Source](https://github.com/floridaman963/geoip2)[ Packagist](https://packagist.org/packages/floridaman963/geoip2)[ RSS](/packages/floridaman963-geoip2/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

GeoIP2 PHP API
==============

[](#geoip2-php-api)

Description
-----------

[](#description)

This package provides an API for the GeoIP2 and GeoLite2 [web services](https://dev.maxmind.com/geoip/docs/web-services?lang=en) and [databases](https://dev.maxmind.com/geoip/docs/databases?lang=en).

Install via Composer
--------------------

[](#install-via-composer)

We recommend installing this package with [Composer](https://getcomposer.org/).

### Download Composer

[](#download-composer)

To download Composer, run in the root directory of your project:

```
curl -sS https://getcomposer.org/installer | php
```

You should now have the file `composer.phar` in your project directory.

### Install Dependencies

[](#install-dependencies)

Run in your project root:

```
php composer.phar require geoip2/geoip2:~2.0
```

You should now have the files `composer.json` and `composer.lock` as well as the directory `vendor` in your project directory. If you use a version control system, `composer.json` should be added to it.

### Require Autoloader

[](#require-autoloader)

After installing the dependencies, you need to require the Composer autoloader from your code:

```
require 'vendor/autoload.php';
```

Install via Phar
----------------

[](#install-via-phar)

Although we strongly recommend using Composer, we also provide a [phar archive](https://php.net/manual/en/book.phar.php) containing most of the dependencies for GeoIP2. Our latest phar archive is available on [our releases page](https://github.com/maxmind/GeoIP2-php/releases).

### Install Dependencies

[](#install-dependencies-1)

In order to use the phar archive, you must have the PHP [Phar extension](https://php.net/manual/en/book.phar.php) installed and enabled.

If you will be making web service requests, you must have the PHP [cURL extension](https://php.net/manual/en/book.curl.php)installed to use this archive. For Debian based distributions, this can typically be found in the the `php-curl` package. For other operating systems, please consult the relevant documentation. After installing the extension you may need to restart your web server.

If you are missing this extension, you will see errors like the following:

```
PHP Fatal error:  Uncaught Error: Call to undefined function MaxMind\WebService\curl_version()

```

### Require Package

[](#require-package)

To use the archive, just require it from your script:

```
require 'geoip2.phar';
```

Optional C Extension
--------------------

[](#optional-c-extension)

The [MaxMind DB API](https://github.com/maxmind/MaxMind-DB-Reader-php)includes an optional C extension that you may install to dramatically increase the performance of lookups in GeoIP2 or GeoLite2 databases. To install, please follow the instructions included with that API.

The extension has no effect on web-service lookups.

IP Geolocation Usage
--------------------

[](#ip-geolocation-usage)

IP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP2 database or web service should not be used to identify a particular address or household.

Database Reader
---------------

[](#database-reader)

### Usage

[](#usage)

To use this API, you must create a new `\GeoIp2\Database\Reader` object with the path to the database file as the first argument to the constructor. You may then call the method corresponding to the database you are using.

If the lookup succeeds, the method call will return a model class for the record in the database. This model in turn contains multiple container classes for the different parts of the data such as the city in which the IP address is located.

If the record is not found, a `\GeoIp2\Exception\AddressNotFoundException`is thrown. If the database is invalid or corrupt, a `\MaxMind\Db\InvalidDatabaseException` will be thrown.

See the API documentation for more details.

### City Example

[](#city-example)

```
