Skip to content

Instantly share code, notes, and snippets.

@dawidbeno
Created April 29, 2025 09:35
Show Gist options
  • Save dawidbeno/c6bcbcb66c55e3455e9539d03c45d67b to your computer and use it in GitHub Desktop.
Save dawidbeno/c6bcbcb66c55e3455e9539d03c45d67b to your computer and use it in GitHub Desktop.
Get location based on IP address

Get country from IP address

Recently I was creating web page which had localization for three different languages. I needed to localize the country from which a user requested my page. Based on the country I wanted to setup localization. English is a default setting but in cases request will come from Germany, the localization should have been set to German. Next option was Slovak language in case request will come from Slovakia.

ipinfo.io API

There are no ranges for IP addresses for each country. Country in which is particular IP address assigned, could change. So we have to rely on services which has access to database with metadata for particular IP. There are several servers, but I have used ipinfo.io

This is how you can obtain IP to work with:

$ip = $_SERVER['REMOTE_ADDR'];

Option 1 : file_get_contents

We can use function file_get_contents() with URL as a parameter to call API to ipinfo service.

$contents = file_get_contents("https://ipinfo.io/".$ip."/json");
$res = json_decode($contents, true);
$country = $res["country"];

However, this option requires allowed option allow_url_fopen. We have to allow this php option to make file_get_contents() work properly with url as a parameter. You can edit php.ini file to change this option.

allow_url_fopen = On

You can also check it by executing phpinfo() or view php.ini file.

phpinfo();

Option 2 : curl

In case you are not able to edit php.ini or to use file_get_contents(), don't worry - there is second option for you. You can use cURL from php.

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "http://ipinfo.io/".$ip."/json");
$contents = curl_exec($ch);

Then you have to extract country from JSON response

$res = json_decode($contents, true);
$country = $res["country"];
<?php
// Get the client's IP address
$ip = $_SERVER['REMOTE_ADDR'];
echo "Request IP: " . $ip . "<br><br>";
echo "<h3>Method 1: Using cURL</h3>";
// Method 1: Using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "http://ipinfo.io/" . $ip . "/json");
$contents_curl = curl_exec($ch);
curl_close($ch);
echo "Content from API call (cURL): " . $contents_curl . "<br>";
$res_curl = json_decode($contents_curl, true);
$country_curl = isset($res_curl["country"]) ? $res_curl["country"] : "";
echo "Country detected (cURL): " . $country_curl . "<br>";
echo "Language setup (cURL): ";
if ($country_curl == "SK") {
echo "Setup Slovak language";
} else if ($country_curl == "DE") {
echo "Setup German language";
} else {
echo "Setup English language";
}
echo "<br><br><h3>Method 2: Using file_get_contents</h3>";
// Method 2: Using file_get_contents
$contents_file = @file_get_contents("https://ipinfo.io/" . $ip . "/json");
if ($contents_file === false) {
echo "Error fetching data with file_get_contents<br>";
} else {
echo "Content from API call (file_get_contents): " . $contents_file . "<br>";
$res_file = json_decode($contents_file, true);
$country_file = isset($res_file["country"]) ? $res_file["country"] : "";
echo "Country detected (file_get_contents): " . $country_file . "<br>";
echo "Language setup (file_get_contents): ";
if ($country_file == "SK") {
echo "Setup Slovak language";
} else if ($country_file == "DE") {
echo "Setup German language";
} else {
echo "Setup English language";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment