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

How to Perform a GeoIP 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 GeoIP providers put many different pieces of data together to build an accurate database of IP location data.

GeoIP 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 GeoIP data that you can easily consume.

Today I'll show you how to use our newly released simple-geoip PHP library to perform a GeoIP 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: https://ip-geolocation.whoisxmlapi.com/signup.

IP Geolocation API is one of the largest and least expensive GeoIP providers. You can use the IP Geolocation API service to perform 1,000 free GeoIP 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 GeoIP 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 GeoIP 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 (`8.8.8.8`, one of Google's core DNS servers):

            
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use WhoisApi\SimpleGeoip\Builders\ClientBuilder;
$builder = new ClientBuilder();
$client = $builder->build('API_KEY');
try {
    echo $client->getRawData('8.8.8.8') . PHP_EOL;
    echo print_r($client->get('8.8.8.8'), 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] => 8.8.8.8
    [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 `8.8.8.8` IP address.

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

Use Your New GeoIP 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 GeoIP look-ups into your product or service!

Some really common use cases for GeoIP 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, GeoIP 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 GeoIP look-ups doesn't have to be hard or expensive. By using our new https://github.com/whois-api-llc/php-simple-geoip 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 GeoIP 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!

Have questions?

We work hard to improve our services for you. As part of that, we welcome your feedback, questions and suggestions. Please let us know your thoughts and feelings, and any way in which you think we can improve our product.

For a quick response, please select the request type that best suits your needs. For more info regarding the request types, see the Contact us page.

By submitting, you agree to the Terms of Service and Privacy Policy.

Or shoot us an email to