Country name from PHP

How to get country name from an IP address using PHP without using a commercial GeoIP Region Edition?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Code

$json = file_get_contents('http://freegeoip.appspot.com/json/66.102.13.106');
$expression = json_decode($json);
print_r($expression);

Result

stdClass Object
(
    [status] => 1
    [ip] => 66.102.13.106
    [countrycode] => US
    [countryname] => United States
    [regioncode] => CA
    [regionname] => California
    [city] => Mountain View
    [zipcode] => 94043
    [latitude] => 37.4192
    [longitude] => -122.057
)

To get countryname

echo $expression->countryname;

Result

United States

Solution 2

I don’t think you can do it simply using PHP, but I have found a free API solution that you can use. It requires simple post and response.
http://www.hostip.info/use.html

example post:
http://api.hostip.info/get_html.php?ip=12.215.42.19

example response:
Country: UNITED STATES (US)
City: Sugar Grove, IL

Solution 3

Use the free IP geolocation webservice

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply