Search code examples
apigeolocationipip-geolocation

Reliable IP to Nation API


I was relying on YQL to do some IP to Nation transformations. That was a mistake!

Are there any Reliable IP to Nation APIs out there that can be accessed via REST or something similar?


Solution

  • PHP function...

    function geoip($ip = "127.0.0.1"){
    
        if($ip == "127.0.0.1"){$ip = $_SERVER["REMOTE_ADDR"];}//if no IP specified use your own
    
        $ch = curl_init();//faster than file_get_contents()
        curl_setopt($ch, CURLOPT_URL,'http://www.geoplugin.net/php.gp?ip='.$ip);//fetch data from geoplugin.net
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $curl = curl_exec($ch);
        curl_close($ch);
    
        $geoip = unserialize($curl);
        return $geoip["geoplugin_countryName"]." ".$geoip["geoplugin_city"];//return country and city
    }
    

    More info about service... http://www.geoplugin.com/webservices/php