Once you subscribe, you will get access to the API dashboard with your API key.
Make a note of this API key.
You can use the built-in curl library to call the IP geolocation API within the PHP script. Here is the complete PHP program.
Copy the code and replace the placeholder <Your_UNIQUE_API_KEY> with your API key before saving the file.
This code accepts an IPv4 IP address and validates it using the built-in php function filter_var( ) validation method.
Open a command line terminal window and run the PHP script with a valid IP address.
You can try with other valid IP addresses to obtain country names. You can also try with an invalid address. The script should catch it.
Getting Country from IPv6 IP Address
If your website visitors have an IPv6 address, there is nothing to worry about. The IP geolocation API can extract country names from IPv6 addresses as well.
Here is the modified PHP script for validating and extracting the country from an IPv6 IP address.
Replace the API key, save the file and run it.
The filter_var( ) validation arguments are now updated to check for valid IPv6 format. Therefore, this PHP script, too, will catch any invalid IP address.
Get Country Specific Local Details from IP Address
The API is not limited to the country name. It can extract many localized details about the IP address's geographic location.
Here is a glimpse of the API JSON response captured from the documentation.
You can see that the API can also find country details, like country code, down to the region and city names for every IP address.
Let’s figure out how to extract these details from API response in PHP.
Get Country Specific Global Details from IP Address
If you look closely at the documentation of the IP geolocation API, it also extracts global details about the country, such as continent, time zone, and geolocation coordinates.
Let's see how to extract these parameters from the API response.
Get Country for Multiple IP Address
This API works for multiple IP addresses. So instead of probing each visitor's IP address with a separate API call, you may want to generate a report of all visitor countries from your web server IP data logs.
You can achieve a report format per your needs by passing multiple IP addresses to the API request. For example, here is how you can do it for a CSV formatted report of IP addresses to country names.
As you can see, the output of this PHP script is a CSV formatted report of visitor IP address mapped to country and continent names.
Based on your requirements, you can modify the report format to deliver an abridged record with country code , region code, and continent code, along with IP data.
Can You Find Country Name from IP Address?
You can find the country name from an IP address, provided the IP address is public and globally addressable. Apart from that, you can also find the city and region names of the IP address within the country. Moreover, with the help of Abstract IP Geolocation API, it is possible to get these details along with timezone, ISP, and location coordinate specific IP address information.
How to find the Country of IP Address?
There is no direct way of decoding the country name from the IP address. But, it is possible to access a database that tracks IP address ranges allocation details across all ISPs operating in various geographies. However, with the help of Abstract IP Geolocation API, it is possible to get this information instantly. IP geolocation API maintains long-standing relationships with ISPs and other data providers to provide highly accurate and consistent country-specific data for IP addresses. Along with the country name, the API also offers valuable information about the location of the IP address, such as region, city, and location coordinates.
Why is IP Address Mapped to Country Name?
IP addresses are allocated to various Internet Service Providers (ISP) in different regions worldwide. All these ISPs operate as autonomous systems, which form the basis for the global routing of Internet traffic to and from all continents. IANA is the body that maintains a set of registries of IP addresses. Both IPv4 and IPv6 addresses are generally assigned hierarchically. ISPs assign users IP addresses. ISPs obtain allocations of IP addresses from a local Internet registry (LIR) or National Internet Registry (NIR), or their appropriate Regional Internet Registry (RIR). Thus all the users are mapped to one of the LIR or NIRs mapped to a country. This arrangement allows for tracking user IP addresses based on country names. If you want complete location details about all the information associated with IP addresses, you can use the Abstract's free Geolocation API.