PHP | Developer libraries | IP Geolocation API | WhoisXML API

IP Geolocation client library in PHP language IP Geolocation client library in PHP language

How to Perform a IP Geolocation Look-up in PHP

In this article, I'm going to walk you through the best possible way to find the physical location of an IP address in PHP language (also known as IP geolocation).

Unfortunately, there are no hard and fast rules for figuring out where an IP address is physically located. Instead, companies referred to as IP Geolocation providers put many different pieces of data together to build an accurate database of IP location data.

IP Geolocation data is typically comprised of:

  • Domain WHOIS data (which itself must be aggregated by data providers)
  • Regional Internet Registries, which hand out large blocks of IP addresses to various Internet Service Providers around the world (ISPs)
  • BGP feeds from large ISPs
  • Latency information (how long it takes for a packet from certain physical locations to reach the destination IP)

While getting all of the above information yourself is very complicated and expensive, there are luckily a few great service providers who've already done this work for you and offer IP Geolocation data that you can easily consume.

Today I'll show you how to use our newly released simple-geoip PHP library to perform a IP Geolocation database look-up and return the physical location of any IP address you might want to pinpoint.

Create a IP Geolocation API Look-up Account

The first thing you'll need to do to use the simple-geoip library is create a free IP Geolocation API account:

IP Geolocation API is one of the largest and least expensive IP Geolocation providers. You can use the IP Geolocation API service to perform 1,000 free IP Geolocation queries each month, or you can pay them a flat fee of $27 per month for 100,000 queries. Extra tariff plans are available here.

Once you've created and logged into your IP Geolocation API account, you'll need to view your account's products page and copy your API key — you will need this later to make IP Geolocation queries.

Install the simple-geoip Package

Now that your account is set up, the next thing you need to do is install the package. From the command line, run the following command:

$ composer require whois-api/simple-geoip

This will download and install the latest release of the simple-geoip package from packagist.

Perform a IP Geolocation Look-up Using simple-geoip

Now that you have both an account and the simple-geoip package installed, let’s take a look at some code you can run to look up the physical address of any IP address you want.

Here’s a small script, `geoip.php`, which will find the physical location of a popular IP address (``, one of Google's core DNS servers):

require_once __DIR__ . '/../vendor/autoload.php';
use WhoisApi\SimpleGeoip\Builders\ClientBuilder;
$builder = new ClientBuilder();
$client = $builder->build('API_KEY');
try {
    echo $client->getRawData('') . PHP_EOL;
    echo print_r($client->get(''), true) . PHP_EOL;
} catch(\Exception $e) {
    echo $e->getMessage();

As you can see, there are really only three steps to using the library:

  • Require the composer’s autoload file.
  • Import the builder which will build a client for you.
  • Build an API client object with the builder by giving your API key that was created when you signed up for the IP Geolocation API service.
  • Run the `getRawData` method, entering the IP address you want to verify. Also, you can specify the result format (json or xml). This method will return a string which contains the result in various formats (json/xml).
  • Run the `get` method entering the IP address you want to get the location of. This method will return a ResponseModel object which contains well parsed data including address, postal code, timezone and so on.

The data that’s returned from the `get` method will look like this:

WhoisApi\SimpleGeoip\Models\Response Object
    [ip] =>
    [location] => WhoisApi\SimpleGeoip\Models\Location Object
            [country] => US
            [region] => California
            [city] => Mountain View
            [lat] => 37.40599
            [lng] => -122.078514
            [postalCode] => 94043
            [timezone] => -07:00

This PHP object tells you everything you need to know about the physical location of the `` IP address.

Behind the scenes, the IP Geolocation API service is handling all the IP Geolocation database look-ups and data aggregation — getting data from providers and processing millions of updates per day.

Use Your New IP Geolocation Data

Now that you’ve seen how easy it is to find the physical location of IP addresses using the simple-geoip library, you should start implementing IP Geolocation look-ups into your product or service!

Some really common use cases for IP Geolocation data include:

  • Detecting a user's country when they visit your website and providing a customized experience for them based on their location (language, ads, design, currency, etc.)
  • Block users from certain locations from accessing your website. For instance, if you're a video streaming provider and only have rights to stream video in a specific country, IP Geolocation look-ups can provide you with that data so you can only serve customers in regions where you can legally operate.
  • Fraud and risk mitigation. If you notice a large amount of fraud coming from a specific location, temporarily blocking visitors from that location could be a quick way to help mitigate fraud and other issues.

By analyzing the IP addresses of visitors to your website you can greatly enhance any web products and services.

Use simple-geoip

To wrap things up: performing IP Geolocation look-ups doesn't have to be hard or expensive. By using our new PHP library and the IP Geolocation API service, you can easily build and manage even a large web product for very little money.

If you need to perform IP Geolocation look-ups, please check out the simple-geoip library as it makes looking up IP address location information incredibly simple.

If you have any questions, please email us!