Hello friends, I was facing issue of getting country name from IP address. Here I am going to tell you how to resolve this issue by passing IP address and retrieving country name. There are many APIs which will do this for you. I am using the service of this site: http://ipinfo.io/
Write the following lines of code here:
$ip = $_SERVER['REMOTE_ADDR'];
$country_details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
echo $country_details->country; // -> "IN"
In the above code:
$_SERVER['REMOTE_ADDR']
: This will give you your current IP address.
file_get_contents:
This is alternative way of curl. It will going to read the content of the site and will give you output
You will get country on this variable $country_details. The output of the above code will be the following:
stdClass Object
(
[ip] => 14.141.58.163
[hostname] => 14.141.58.163.static-delhi.vsnl.net.in
[city] =>
[region] =>
[country] => IN
[loc] => 20.0000,77.0000
[org] => AS4755 TATA Communications formerly VSNL is Leading ISP
)
Hope this will going to help. If you have any other ways to do this, then please don't forget to mention it on the comment.
Thanks for reading the blog.
0 Comment(s)