WC_Integration_MaxMind_Database_Service::get_iso_country_code_for_ip()publicWC 1.0

Fetches the ISO country code associated with an IP address.

Метод класса: WC_Integration_MaxMind_Database_Service{}

Хуков нет.

Возвращает

Строку. The country code for the IP address, or empty if not found.

Использование

$WC_Integration_MaxMind_Database_Service = new WC_Integration_MaxMind_Database_Service();
$WC_Integration_MaxMind_Database_Service->get_iso_country_code_for_ip( $ip_address );
$ip_address(строка) (обязательный)
The IP address to find the country code for.

Код WC_Integration_MaxMind_Database_Service::get_iso_country_code_for_ip() WC 9.3.3

public function get_iso_country_code_for_ip( $ip_address ) {
	$country_code = '';

	if ( ! class_exists( 'MaxMind\Db\Reader' ) ) {
		wc_get_logger()->notice( __( 'Missing MaxMind Reader library!', 'woocommerce' ), array( 'source' => 'maxmind-geolocation' ) );
		return $country_code;
	}

	$database_path = $this->get_database_path();
	if ( ! file_exists( $database_path ) ) {
		return $country_code;
	}

	try {
		$reader = new MaxMind\Db\Reader( $database_path );
		$data   = $reader->get( $ip_address );

		if ( isset( $data['country']['iso_code'] ) ) {
			$country_code = $data['country']['iso_code'];
		}

		$reader->close();
	} catch ( Exception $e ) {
		wc_get_logger()->notice( $e->getMessage(), array( 'source' => 'maxmind-geolocation' ) );
	}

	return $country_code;
}