IP2Location PHP API#

Database Class#

class Database($file, $mode)#

Initiate the IP2Location class and load the IP2Location BIN database.

Parameters:
  • $file (str) – (Required) The file path links to IP2Location BIN databases.

  • $mode (str) – (Optional) The file mode used to open the IP2Location BIN database. Options available are FILE_IO, SHARED_MEMORY and MEMORY_CACHE. Default is FILE_IO.

lookup($ip)#

Retrieve geolocation information for an IP address.

Parameters:

$ip (str) – (Required) The IP address (IPv4 or IPv6).

Returns:

Returns the geolocation information in dict. Refer below table for the fields avaliable in the dict

Return type:

array

RETURN FIELDS

Field Name

Description

countryCode

Two-character country code based on ISO 3166.

countryName

Country name based on ISO 3166.

regionName

Region or state name.

cityName

City name.

isp

Internet Service Provider or company’s name.

latitude

City latitude. Defaults to capital city latitude if city is unknown.

longitude

City longitude. Defaults to capital city longitude if city is unknown.

domainName

Internet domain name associated with IP address range.

zipCode

ZIP code or Postal code. 172 countries supported.

timeZone

UTC time zone (with DST supported).

netSpeed

Internet connection type.

iddCode

The IDD prefix to call the city from another country.

areaCode

A varying length number assigned to geographic areas for calls between cities. 223 countries supported.

weatherStationCode

The special code to identify the nearest weather observation station.

weatherStationName

The name of the nearest weather observation station.

mcc

Mobile Country Codes (MCC) as defined in ITU E.212 for use in identifying mobile stations in wireless telephone networks, particularly GSM and UMTS networks.

mnc

Mobile Network Code (MNC) is used in combination with a Mobile Country Code(MCC) to uniquely identify a mobile phone operator or carrier.

mobileCarrierName

Commercial brand associated with the mobile carrier. You may click mobile carrier coverage to view the coverage report.

elevation

Average height of city above sea level in meters (m).

usageType

Usage type classification of ISP or company.

addressType

IP address types as defined in Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6).

category

The domain category based on IAB Tech Lab Content Taxonomy.

district

District or county name.

asn

Autonomous system number (ASN). BIN databases.

as

Autonomous system (AS) name.

IpTools Class#

class IpTools#

Initiate IpTools class.

isIpv4($ip)#

Verify if a string is a valid IPv4 address.

Parameters:

$ip (str) – (Required) IP address.

Returns:

Return True if the IP address is a valid IPv4 address or False if it isn’t a valid IPv4 address.

Return type:

boolean

isIpv6($ip)#

Verify if a string is a valid IPv6 address

Parameters:

$ip (str) – (Required) IP address.

Returns:

Return True if the IP address is a valid IPv6 address or False if it isn’t a valid IPv6 address.

Return type:

boolean

ipv4ToDecimal($ip)#

Translate IPv4 address from dotted-decimal address to decimal format.

Parameters:

$ip (str) – (Required) IPv4 address.

Returns:

Return the decimal format of the IPv4 address.

Return type:

int

decimalToIpv4($number)#

Translate IPv4 address from decimal number to dotted-decimal address.

Parameters:

$number (str) – (Required) Decimal format of the IPv4 address.

Returns:

Returns the dotted-decimal format of the IPv4 address.

Return type:

string

ipv6ToDecimal($ip)#

Translate IPv6 address from hexadecimal address to decimal format.

Parameters:

$ip (str) – (Required) IPv6 address.

Returns:

Return the decimal format of the IPv6 address.

Return type:

int

decimalToIpv6($number)#

Translate IPv6 address from decimal number into hexadecimal address.

Parameters:

$number (str) – (Required) Decimal format of the IPv6 address.

Returns:

Returns the hexadecimal format of the IPv6 address.

Return type:

string

ipv4ToCidr($ipFrom, $ipTo)#

Convert IPv4 range into a list of IPv4 CIDR notation.

Parameters:
  • $ipFrom (str) – (Required) The starting IPv4 address in the range.

  • $ipTo (str) – (Required) The ending IPv4 address in the range.

Returns:

Returns the list of IPv4 CIDR notation.

Return type:

list

cidrToIpv4($cidr)#

Convert IPv4 CIDR notation into a list of IPv4 addresses.

Parameters:

$cidr (str) – (Required) IPv4 CIDR notation.

Returns:

Returns an array of IPv4 addresses.

Return type:

dict

ipv6ToCidr($ipFrom, $ipTo)#

Convert IPv6 range into a list of IPv6 CIDR notation.

Parameters:
  • $ipFrom (str) – (Required) The starting IPv6 address in the range.

  • $ipTo (str) – (Required) The ending IPv6 address in the range.

Returns:

Returns the list of IPv6 CIDR notation.

Return type:

list

cidrToIpv6($cidr)#

Convert IPv6 CIDR notation into a list of IPv6 addresses.

Parameters:

$cidr (str) – (Required) IPv6 CIDR notation.

Returns:

Returns an array of IPv6 addresses.

Return type:

dict

compressIpv6($ip)#

Compress a IPv6 to shorten the length.

Parameters:

$ip (str) – (Required) IPv6 address.

Returns:

Returns the compressed version of IPv6 address.

Return type:

str

expandIpv6($ip)#

Expand a shorten IPv6 to full length.

Parameters:

$ip (str) – (Required) IPv6 address.

Returns:

Returns the extended version of IPv6 address.

Return type:

str

getVisitorIp($ipData)#

Return the real IP address of the visitor. If an array of $ipData is supplied, it will return the list of IP address data found.

Parameters:

$ipData (array) – (Optional) List of IP addresses.

Returns:

Returns the extended version of IPv6 address.

Return type:

str

Country Class#

class Country($csv)#

Initiate Country class and load the IP2Location Country Information CSV file. This database is free for download at https://www.ip2location.com/free/country-information.

Parameters:

$csv (str) – (Required) The file path links to IP2Location Country Information CSV file.

getCountryInfo($countryCode)#

Provide a ISO 3166 country code to get the country information in array. Will return a full list of countries information if country code not provided.

Parameters:

$countryCode (str) – (Required) The ISO 3166 country code of a country.

Returns:

Returns the country information in dict. Refer below table for the fields avaliable in the dict.

Return type:

dict

RETURN FIELDS

Field Name

Description

country_code

Two-character country code based on ISO 3166.

country_alpha3_code

Three-character country code based on ISO 3166.

country_numeric_code

Three-character country code based on ISO 3166.

capital

Capital of the country.

country_demonym

Demonym of the country.

total_area

Total area in km2.

population

Population of year 2014.

idd_code

The IDD prefix to call the city from another country.

currency_code

Currency code based on ISO 4217.

currency_name

Currency name.

currency_symbol

Currency symbol.

lang_code

Language code based on ISO 639.

lang_name

Language name.

cctld

Country-Code Top-Level Domain.

Region Class#

class Region($csv)#

Initiate Region class and load the IP2Location ISO 3166-2 Subdivision Code CSV file. This database is free for download at https://www.ip2location.com/free/iso3166-2

Parameters:

$csv (str) – (Required) The file path links to IP2Location ISO 3166-2 Subdivision Code CSV file.

getRegionCode($countryCode, $regionName)#

Provide a ISO 3166 country code and the region name to get ISO 3166-2 subdivision code for the region.

Parameters:
  • $countryCode (str) – (Required) Two-character country code based on ISO 3166.

  • $regionName (str) – (Required) Region or state name.

Returns:

Returns the ISO 3166-2 subdivision code of the region.

Return type:

str