You are here

public function GeoLocation::geolocate in GeoIP API 8.2

Geolocate an IP address.

Parameters

string $ip_address: The IP address to geo locate.

Return value

string|null The geolocated country code, or NULL if not found.

File

src/GeoLocation.php, line 83

Class

GeoLocation
Service to interact with the default geolocator plugin for geolocation.

Namespace

Drupal\geoip

Code

public function geolocate($ip_address) {
  if (!isset($this->locatedAddresses[$ip_address])) {
    if ($cache = $this->cacheBackend
      ->get($this->cacheKey . ':' . $ip_address)) {
      $this->locatedAddresses[$ip_address] = $cache->data;
    }
    else {
      $geolocator = $this
        ->getGeoLocator();
      $result = $geolocator
        ->geolocate($ip_address);
      $this->locatedAddresses[$ip_address] = $result;
      $this->cacheBackend
        ->set($this->cacheKey . ':' . $ip_address, $this->locatedAddresses[$ip_address], Cache::PERMANENT, $this->cacheTags);
    }
  }
  return $this->locatedAddresses[$ip_address];
}