A PHP library for interfacing with IPStack Geo API

v1.6.4 2019-11-26 17:23 UTC

This package is auto-updated.

Last update: 2021-01-12 20:41:11 UTC


IPStack for PHP is a simple library used to interface with an IPStack Geo API.

$ composer require nafisc/ipstackgeo-php

Maintainability StyleCI TravisCI Coverage Status Latest Stable Version Total Downloads License

Learn more about IPStack here:

Looking for the Python version?


  • Retrieve the Geo Location data for any IP address.
  • Retrieve the Geo Location data for the system executing this code.
  • Retrieve the Geo Location data for a client.
  • Retrieve the Geo Location data for a batch of IP addresses.
  • Assess the security of an IP address.

Legacy Features

  • Link to a custom FreeGeoIP server

Basic Usage

$geo = new GeoLookup('.....');
$location = $geo->getLocation('');

Example Usage

Note: See IPStack: Response Objects for a list of available properties in a response object.

Create the GeoLookup object

use IPStack\PHP\GeoLookup;

// Create the GeoLookup object using your API key.
$geoLookup = new GeoLookup('acecac3893c90871c3');

Lookup a location for an IP Address

// Lookup a location for an IP Address
// and catch any exceptions that might
// be thrown by Guzzle or IPStack.
try {
    // Retrieve the location information for 
    // by using it's hostname.
    // This function will work with hostnames
    // or IP addresses.
    $location = $geoLookup->getLocation('');

    // If we are unable to retrieve the location information
    // for an IP address, null will be returned.
    if (\is_null($location)) {
        echo 'Failed to find location.'.PHP_EOL;
    } else {
        // Print the Location Object.
} catch (\Exception $e) {
    echo $e->getMessage();

Look up a Clients location

$location = $geoLookup->getClientLocation();

Look up own location

$location = $geoLookup->getOwnLocation();

Other Features

There are also a few other useful features built into this library and the IPStack API.

  1. Bulk Location Lookup

    The ipstack API also offers the ability to request data for multiple IPv4 or IPv6 addresses at the same time. This requires the PROFESSIONAL teir API key or higher and is limitted to 50 IPs at a time.


    $lookup = ['', '', ''];
    $locations = $geoLookup->getLocations(...$lookup);
  2. Requesting the hostname for an IP address.

    By default, the ipstack API does not return information about the hostname the given IP address resolves to. In order to include the hostname use the following.


    $location = $geoLookup->setFindHostname(true)->getLocation('');
    echo $location['hostname'];
  3. Assessing Security

    Customers subscribed to the Professional Plus Plan may access the ipstack API's Security Module, which can be used to assess risks and threats originating from certain IP addresses before any harm can be done to a website or web application.


    $location = $geoLookup->assessSecurity(true)->getLocation('');
  4. Set the language for a response

    The ipstack API is capable of delivering its result set in different languages. To request data in a language other than English (default) use following with one of the supported language codes.


    Supported Langauges

    $location = $geoLookup->setLanguage('en')->getLocation('');
  5. Configuring your request

    /// Use HTTPS
    /// This requires IPStack Basic plan or higher.
    $location = $geoLookup->useHttps(true)->getLocation('');
    /// Configure the timeout for requests
    $location = $geoLookup->setTimeout(15)->getLocation('');

Using the the Legacy FreeGeoIP Binary

You can still use the legacy FreeGeoIP Binary hosted on a server

Note: FreeGeoIP has been deprecated.

use IPStack\PHP\Legacy\FreeGeoIp as GeoLookup;

// Address, Port, Protocol, Timeout
$geoLookup = new GeoLookup(
    'localhost', // Address hosting the legacy FreeGeoIP Binary
    8080,        // Port that the binary is running on (defaults to 8080)
    'http',      // Protocol to use (defaults to http)
    10           // Timeout (defaults to 10 seconds)