You are here

public function Ip2CountryCommands::lookup in IP-based Determination of a Visitor's Country 8

Finds the country associated with the given IP address.

@command ip2country:lookup @aliases ip-lookup,ip2country-lookup

@usage drush ip2country:lookup IPV4 Returns a country code associated with the given IP address. @usage drush ip2country:lookup IPV4 --field=name Returns Country name for the IP address.

@table-style default @field-labels ip_address: IP address name: Country country_code_iso2: Country code @default-fields ip_address,name,country_code_iso2

@validate-module-enabled ip2country

Parameters

string $ip_address: The IPV4 address to look up, in dotted-quad notation (e.g. 127.0.0.1).

Return value

\Consolidation\OutputFormatters\StructuredData\RowsOfFields Returns the country name and two-character ISO 3166 country code associated with the given IP address.

File

src/Commands/Ip2CountryCommands.php, line 190

Class

Ip2CountryCommands
Drush 9+ commands for the IP2Country module.

Namespace

Drupal\ip2country\Commands

Code

public function lookup($ip_address) {
  $country_code = $this->ip2countryLookup
    ->getCountry($ip_address);
  $rows = [];
  if ($country_code == FALSE) {
    $this->output
      ->writeln(dt('IP address not found in the database.'));
  }
  else {
    $country_list = $this->countryManager
      ->getList();
    $country_name = $country_list[$country_code];
    $rows[$ip_address] = [
      'ip_address' => $ip_address,
      'name' => (string) dt($country_name),
      'country_code_iso2' => $country_code,
    ];
  }
  return new RowsOfFields($rows);
}