Skip to main content

How to Integrate IP Geolocation API with PHP

Geolocation is the identification of an object in the real world Geographic location. The Geolocation data includes details of latitude, longitude, address, country code, country, zip code and more.

The Geolocation data is very useful as it is used by businesses to know about their customers to target the with relevant information. Due to importance, most of the businesses always looking for the accurate Geolocation data. It’s not very easy to get the accurate Geolocation data. But don’t be worry, now it’s very simple with Geolocation API.

So if you’re looking for solution to get the Geolocation data then you’re here at the right place. In this tutorial, you will learn how to get the geolocation data with IP Geolocation API using PHP.

The IP Geolocation API is free and easy to use API to get geolocation data. The API returns geolocation data into JSON format and can be integrated with any major programming language. It’s provides Geolocation data from IP Address and country.

Also, read:

So let’s start integrating the IP Geolocation API with PHP.

Step1: Geolocation Data By IP Address

The API is very easy to integrate to get the geolocation data. There is API URL to use without passing IP address as it’s automatically detect the user IP address in the frontend and return geolocation data.

https://api.ipgeolocationapi.com/geolocate

We can also pass the user IP address with geolocate API to get the geolocation data of a specific IP address.

https://api.ipgeolocationapi.com/geolocate/91.213.103.0

Now we will integrate the API with PHP by making HTTP GET request to API with PHP Curl to get geolocation data of IP Address.

<?php

$ip_address = '91.213.103.0';

$ch = curl_init("https://api.ipgeolocationapi.com/geolocate/".$ip_address);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultData = curl_exec($ch);
curl_close($ch);

$jsonData = json_encode(json_decode($resultData), JSON_PRETTY_PRINT);
echo $jsonData;

?>

Here is the Geolocation JSON data returned by above code for the IP address 91.213.103.0.

{
    "continent": "Europe",
    "address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
    "alpha2": "DE",
    "alpha3": "DEU",
    "country_code": "49",
    "international_prefix": "00",
    "ioc": "GER",
    "gec": "GM",
    "name": "Germany",
    "national_destination_code_lengths": [
        2,
        3,
        4,
        5
    ],
    "national_number_lengths": [
        6,
        7,
        8,
        9,
        10,
        11
    ],
    "national_prefix": "0",
    "number": "276",
    "region": "Europe",
    "subregion": "Western Europe",
    "world_region": "EMEA",
    "un_locode": "DE",
    "nationality": "German",
    "eu_member": true,
    "eea_member": true,
    "vat_rates": {
        "standard": 19,
        "reduced": [
            7
        ],
        "super_reduced": null,
        "parking": null
    },
    "postal_code": true,
    "unofficial_names": [
        "Germany",
        "Deutschland",
        "Allemagne",
        "Alemania",
        "\u30c9\u30a4\u30c4",
        "Duitsland"
    ],
    "languages_official": [
        "de"
    ],
    "languages_spoken": [
        "de"
    ],
    "geo": {
        "latitude": 51.165691,
        "latitude_dec": "51.20246505737305",
        "longitude": 10.451526,
        "longitude_dec": "10.382203102111816",
        "max_latitude": 55.0815,
        "max_longitude": 15.0418962,
        "min_latitude": 47.2701115,
        "min_longitude": 5.8663425,
        "bounds": {
            "northeast": {
                "lat": 55.0815,
                "lng": 15.0418962
            },
            "southwest": {
                "lat": 47.2701115,
                "lng": 5.8663425
            }
        }
    },
    "currency_code": "EUR",
    "start_of_week": "monday"
}

Step2: Geolocation Data by Country

The API also provides country information. So we will also integrate the country API to get the geolocation of country. We will use the following API URL to get the countries list.

https://api.ipgeolocationapi.com/countries

The API takes the country name to return the geolocation data for specific country. Below is the sample API URL to get the country geolocation data.

https://api.ipgeolocationapi.com/countries/us

Now we will integrate the Geolocation API with PHP to get the country geolocation data by country name. We will make HTTP GET API request with PHP Curl and pass the country name to get the country geolocation data.

<?php

$countryName = 'US';

$ch = curl_init("https://api.ipgeolocationapi.com/countries/".$countryName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultData = curl_exec($ch);
curl_close($ch);

$jsonData = json_encode(json_decode($resultData), JSON_PRETTY_PRINT);
echo $jsonData;

?>

The above will return following geolocation response JSON data for the US.

{
    "continent": "North America",
    "address_format": "{{recipient}}\n{{street}}\n{{city}} {{region_short}} {{postalcode}}\n{{country}}",
    "alpha2": "US",
    "alpha3": "USA",
    "country_code": "1",
    "international_prefix": "011",
    "ioc": "USA",
    "gec": "US",
    "name": "United States of America",
    "national_destination_code_lengths": [
        3
    ],
    "national_number_lengths": [
        10
    ],
    "national_prefix": "1",
    "number": "840",
    "region": "Americas",
    "subregion": "Northern America",
    "world_region": "AMER",
    "un_locode": "US",
    "nationality": "American",
    "postal_code": true,
    "unofficial_names": [
        "United States",
        "Vereinigte Staaten von Amerika",
        "\u00c9tats-Unis",
        "Estados Unidos",
        "\u30a2\u30e1\u30ea\u30ab\u5408\u8846\u56fd",
        "Verenigde Staten"
    ],
    "languages_official": [
        "en"
    ],
    "languages_spoken": [
        "en"
    ],
    "geo": {
        "latitude": 37.09024,
        "latitude_dec": "39.44325637817383",
        "longitude": -95.712891,
        "longitude_dec": "-98.95733642578125",
        "max_latitude": 71.5388001,
        "max_longitude": -66.885417,
        "min_latitude": 18.7763,
        "min_longitude": 170.5957,
        "bounds": {
            "northeast": {
                "lat": 71.5388001,
                "lng": -66.885417
            },
            "southwest": {
                "lat": 18.7763,
                "lng": 170.5957
            }
        }
    },
    "currency_code": "USD",
    "start_of_week": "sunday"
}

Step3: Conclusion

Here in this tutorial, we have explained how to integrate IP Geolocation API with PHP to get the geolocation data with IP address and country. You can also checkout the IP Geolocation API code to use further with any other programming language.

You may also like: