Net_GeoIP::lookupCountryName()
Synopsis
string lookupCountryName() (
string $addr
)
Описание
This method returns the full country name for the given IP address. It works with both the free and the non-free databases.
Looking up the country name
<?php
require_once "Net/GeoIP.php";
$geoip = Net_GeoIP::getInstance("/path/to/geoipdb.dat");
try {
echo $geoip->lookupCountryName($_SERVER['REMOTE_ADDR']);
} catch (Exception $e) {
// Handle exception
}
?>
Parameter
-
string $addr
- IP addressLookups on HostnamesNote that this PHP API does NOT support lookups on hostnames. This is so that the public API can be kept simple and so that the lookup functions don't need to try name lookups if IP lookup fails (which would be the only way to keep the API simple and support name-based lookups).
If you do not know the IP address, you can convert an name to IP very simply using PHP native functions or other libraries:
<?php
$geoip->lookupCountryName(gethostbyname("example.com"));
?>Or, if you don't know whether an address is a name or IP address, use application-level logic:
<?php
if (ip2long($ip_or_name) === false) {
$ip = gethostbyname($ip_or_name);
} else {
$ip = $ip_or_name;
}
$country = $geoip->lookupCountryName($ip);
?>
Throws
This method sthrow an exception if the IP address is invalid or if the database type is incorrect.