I want to get visitors country via their IP... Right now I'm using this (http://api.hostip.info/country.php?ip=...... )
Here is my code:
<?php
if (isset($_SERVER['HTTP_CLIENT_IP']))
{
$real_ip_adress = $_SERVER['HTTP_CLIENT_IP'];
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$real_ip_adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$real_ip_adress = $_SERVER['REMOTE_ADDR'];
}
$cip = $real_ip_adress;
$iptolocation = 'http://api.hostip.info/country.php?ip=' . $cip;
$creatorlocation = file_get_contents($iptolocation);
?>
Well, it's working properly, but the thing is, this returns the country code like US or CA., and not the whole country name like United States or Canada.
So, is there any good alternative to hostip.info offers this?
I know that I can just write some code that will eventually turn this two letters to whole country name, but I'm just too lazy to write a code that contains all countries...
P.S: For some reason I don't want to use any ready made CSV file or any code that will grab this information for me, something like ip2country ready made code and CSV.
The User Country API has exactly what you need. Here is a sample code using file_get_contents() as you originally do:
You can use a simple API from http://www.geoplugin.net/
Function Used
Output
It makes you have so many options you can play around with
Thanks
:)
Many different ways to do it...
Solution #1:
One third party service you could use is http://ipinfodb.com. They provide hostname, geolocation and additional information.
Register for an API key here: http://ipinfodb.com/register.php. This will allow you to retrieve results from their server, without this it will not work.
Copy and past the following PHP code:
Downside:
Quoting from their website:
Solution #2:
This function will return country name using the http://www.netip.de/ service.
Output: