En las respuestas anteriores o no funciona la web o no es muy preciso el servicio. En mi caso me sitúa en Alcobendas, Madrid y no es el caso.
Esta otra me gusta bastante https://www.geoplugin.net
Haciendo la siguiente llamada podrás obtener la información d euna ip en formato json:
echo file_get_contents('http://www.geoplugin.net/json.gp?ip=DIRECCION_IP');
Tienes más información en este artículo: https://academy.leewayweb.com/como-restringir-el-acceso-a-una-web-segun-el-pais-del-visitante/
Acorde al comentario de Pablo Gutierrez, Edito y actualizo:
//Obtener IP del visitante
function get_client_ip_server()
{
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
$ipVisitante = get_client_ip_server();
if ($ipVisitante!='UNKNOWN')
{
$data = json_decode( file_get_contents('http://www.geoplugin.net/json.gp?ip='.$ipVisitante, true ));
$bannedCountries = array('AR', 'UY' );
if ( in_array( $data->geoplugin_countryCode, $bannedCountries ) )
{
echo 'No se admite gente de '.$data->geoplugin_countryName;
}
else
{
echo 'Los visitantes de '.$data->geoplugin_countryName.' siempre son bienvenidos :)';
}
}
else
{
echo "IP desconocida";
}