IP Geolocation API Integration with PHP

Geolocation data of users plays an important role for the success of any website or business. With accurate Geolocation data, we can easily target the users with relevant information and content.

It’s always a challenge to get accurate Geolocation data. We sometimes try to develop our own system or sometimes tried many other services that provides Geolocation info but it’s too costly or short of valuable information.

So if you’re a developer or running a business and looking for solution to get Geolocation information of their users, then don’t be worry about this. It’s very easy and simple to get the accurate Geolocation data with IP Geolocation API. In this tutorial you will learn how to use IP Geolocation API with PHP to get the Geolocation data.

Also, read:

IP Geolocation API is free that provides Geolocation data with country information. The API can be used with to get geolocation data using IP address and country.


So let’s start integrating IP Geolocation API to get geolocation data.

Step1: Get Geolocation Data By IP Address

We can easily get the Geolocation information using IP Address. We can use the following API URL for geolocating users in the frontend without the need to detect their actual IP yourself.

https://api.ipgeolocationapi.com/geolocate

Here is the API URL to get the geolocation data by IP Address

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

Here we will make HTTP GET request to Geolocation API with PHP Curl to get the geolocation data of IP Address.

<?php

$ipAddress = '91.213.103.0';

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

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

?>

The above code will return following Geolocation JSON data from IP address.


{
    "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: Get Geolocation Data by Country Name

We can also get the country geolocation information by passing country code to get the country information. Here is the API URL to get the all country list.

https://api.ipgeolocationapi.com/countries

We can pass the country name to get geolocation of a specific country.

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

We will integrate the API with PHP to get the country information by passing country name. We will make HTTP GET API request with PHP Curl and pass country name to get the country information.

<?php

$country = 'FR';

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

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

?>

The above will return following geolocation response JSON data of a country.

{
    "continent": "Europe",
    "address_format": "{{recipient}}\n{{street}}\n{{postalcode}} {{city}}\n{{country}}",
    "alpha2": "FR",
    "alpha3": "FRA",
    "country_code": "33",
    "international_prefix": "00",
    "ioc": "FRA",
    "gec": "FR",
    "name": "France",
    "national_destination_code_lengths": [
        1
    ],
    "national_number_lengths": [
        9,
        10
    ],
    "national_prefix": "0",
    "number": "250",
    "region": "Europe",
    "subregion": "Western Europe",
    "world_region": "EMEA",
    "un_locode": "FR",
    "nationality": "French",
    "eu_member": true,
    "eea_member": true,
    "vat_rates": {
        "standard": 20,
        "reduced": [
            5.5,
            10
        ],
        "super_reduced": 2.1,
        "parking": null
    },
    "postal_code": true,
    "unofficial_names": [
        "France",
        "Frankreich",
        "the French Republic",
        "\u30d5\u30e9\u30f3\u30b9",
        "Frankrijk",
        "Francia"
    ],
    "languages_official": [
        "fr"
    ],
    "languages_spoken": [
        "fr"
    ],
    "geo": {
        "latitude": 46.227638,
        "latitude_dec": "46.63727951049805",
        "longitude": 2.213749,
        "longitude_dec": "2.3382623195648193",
        "max_latitude": 51.1241999,
        "max_longitude": 9.6624999,
        "min_latitude": 41.3253001,
        "min_longitude": -5.5591,
        "bounds": {
            "northeast": {
                "lat": 51.1241999,
                "lng": 9.6624999
            },
            "southwest": {
                "lat": 41.3253001,
                "lng": -5.5591
            }
        }
    },
    "currency_code": "EUR",
    "start_of_week": "monday"
}

Step3: Conclusion

Here we have explained how to use IP Geolocation API with PHP to get the geolocation data by IP address and country name. You can checkout the API repository IP Geolocation API to inspect the code and use further with any other programming language.


You may also like:

2 thoughts on “IP Geolocation API Integration with PHP

Leave a Reply

Your email address will not be published. Required fields are marked *