How to retrieve location information in PHP

Last edited on February 02, 2020

There have been many services available with which to retrieve a clients GeoLocation, the most common that I have seen being FreeGeoIP. Today I’d like to share how you can go about retrieving this information using PHP.

It is worth noting that FreeGeoIP has been deprecated and IPStack has taken it’s place. Luckily, the library I will be using in this post supports both FreeGeoIP and IPStack!

Firstly we need to make a decision. Do we want to use the deprecated FreeGeoIP binary, or IPStack’s API.

Using IPStack

IPStack will be the easiest solution for retrieving a clients Geo information.

To begin with, create an IPStack API key here. That’s it. You’re done. You can now begin to implement the PHP library.

Using FreeGeoIP Binary

If you’ve chosen to use the FreeGeoIP binary, you will need to download it from here. Version 3.4.1 is the last version that has been released at the time this document is being written, and will likely be the last version ever released as FreeGeoIP is now deprecated.

Once you’ve downloaded the FreeGeoIP binary, you will need to start it on your web server.

./freegeoip -http :8080

This will start the binary on port 8080, which you will later be configuring your PHP library to communicate with. It would be a good idea to have this binary run as a service.

At this point, you can already start looking up location information by opening a web browser and going to: _http://

You can now begin to implement the PHP library.

Integrating the PHP Library

Integrating the PHP library is much easier than you’d expect. Firstly, you will need to include the composer package used to retrieve the location information. _(If you aren’t familiar with composer, see _[here](https://getcomposer.org/doc/01-basic-usage.md)_.)_

composer require nafisc/ipstackgeo-php

You can find this PHP library hosted on GitHub here.

Next, you will need to create the object that PHP will use to communicate with the servers that tell us the clients location.

If you are using IPStack

use IPStack\PHP\GeoLookup;

$geoLookup = new GeoLookup(
    // API Key

    // Use HTTPS (Basic plan and up only, default false)

    // Timeout in seconds (defaults to 10 seconds)

If you are using the FreeGeoIP Binary

use IPStack\PHP\Legacy\FreeGeoIp as GeoLookup;

$geoLookup = new GeoLookup(
    // Address hosting the legacy FreeGeoIP Binary

    // Port that the binary is running on (defaults to 8080)

    // Protocol to use (defaults to http)

    // Timeout (defaults to 10 seconds)

Next, you will need to write the code necessary to retrieve the client location.

$location = $geoLookup->getClientLocation();

if ($location == null) {
    echo 'Failed to find location.'.PHP_EOL;
} else {
    // Convert the location to a standard PHP array.

    // Any of these formats will work for
    // retrieving a property.
    echo $location->latitude . PHP_EOL;
    echo $location['longitude'] . PHP_EOL;
    echo $location->region_name() . PHP_EOL;

You can also retrieve the location for any IP address or host-name using this library. See below for an example of this.

See here for a list of available properties on the $location object.

A finished implementation of this library might look something like this

See Gist

Nathan Fiscaletti
Nathan Fiscaletti
Software Engineer