You are here

function drush_ip2country_lookup in IP-based Determination of a Visitor's Country 8

Same name and namespace in other branches
  1. 7 ip2country.drush.inc \drush_ip2country_lookup()

Implements drush_hook_COMMAND() for the ip2country-lookup command.

Gets the ISO 3166 2-character country code from the IP address.

Parameters

string|int $ip_address: IP address either as a dotted quad string (e.g. '127.0.0.1') or as a 32-bit unsigned long integer.

File

./ip2country.drush.inc, line 186
ip2country module integration with Drush 8 and earlier.

Code

function drush_ip2country_lookup($ip_address) {
  $country_code = \Drupal::service('ip2country.lookup')
    ->getCountry($ip_address);
  if ($country_code == FALSE) {
    drush_print(dt('IP address not found in the database.'));
  }
  else {
    $country_list = \Drupal::service('country_manager')
      ->getList();
    $country_name = $country_list[$country_code];
    return [
      $ip_address => [
        'ip_address' => $ip_address,
        'name' => (string) dt($country_name),
        'country_code_iso2' => $country_code,
      ],
    ];
  }
}