[Official Release] IP2Location PHP API to get location info from IPv4 and IPv6 address.
This is the official release maintained by IP2Location.com
This PHP module provides fast lookup of country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection speed, IDD code, area code, weather station code, weather station name, MNC, MCC, mobile brand, elevation, usage type, address type and IAB category from IP address by using IP2Location database. This module uses a file based database available at IP2Location.com.
This module can be used in many types of projects such as:
- select the geographically closest mirror
- analyze your web server logs to determine the countries of your visitors
- credit card fraud detection
- software export controls
- display native language and currency
- prevent password sharing and abuse of service
- geotargeting in advertisement
Free IP2Location LITE and commerical databases are available for download.
- LITE database is available at https://lite.ip2location.com (Free with limited accuracy)
- Commercial database is availabe at https://www.ip2location.com (Comprehensive with high accuracy)
Monthly update is available for both IP2Location LITE and commercial database.
- Support both IPv4 and IPv6 with ease. If you would like to enable IPv6 support, you just need to replace your BIN file with IPv6 version. That's it, and no code modification needed.
- Extensible. If you require different granularity of IP information, you can visit IP2Location.com to download the relevant BIN file, and the information will made ready for you.
- Comprehensive Information. There are more than 13 types of information that you can retrieve from an IP address. Please visit IP2Location.com for details.
Install this package using composer as below:
composer require ip2location/ip2location-php
To test this installation, please browse examples/example.php using web browser.
You can check the example.php file to learn more about usage.
Below is the description of the functions available in the Database class.
|Constructor||Expect 2 input parameters:
|string getDate()||Return the database's compilation date as a string of the form 'YYYY-MM-DD'|
|string getType()||Return the database's type, 1 to 25 respectively for DB1 to DB25. Please visit https://www.ip2location.com/databases for details.|
|string getModuleVersion()||Return the version of module|
|string getDatabaseVersion()||Return the version of database|
|array lookup($ip)||Return the IP information in array. Below is the information returned:
|array getCidr($ip)||Return an array of the complete IP list in CIDR format of the detected row record based on the given IP address.|
Below is the description of the functions available in the WebService class.
|Constructor||Expect 3 input parameters:
|array lookup($ip)||Return the IP information in array.
|int getCredit()||Return remaining credit of the web service account.|
Below is the description of the functions available in the IpTools class.
|bool isIpv4($ip)||Return either true or false. Verify if a string is a valid IPv4 address.|
|bool isIpv6($ip)||Return either true or false. Verify if a string is a valid IPv6 address.|
|mixed ipv4ToDecimal($ip)||Translate IPv4 address from dotted-decimal address to decimal format. Return null on error.|
|mixed decimalToIpv4($number)||Translate IPv4 address from decimal number to dotted-decimal address. Return null on error.|
|mixed ipv6ToDecimal($ip)||Translate IPv6 address from hexadecimal address to decimal format. Return null on error.|
|mixed decimalToIpv6($number)||Translate IPv6 address from decimal number into hexadecimal address. Return null on error.|
|array ipv4ToCidr($ipFrom, $ipTo)||Convert IPv4 range into a list of IPv4 CIDR notation.|
|array cidrToIpv4($cidr)||Convert IPv4 CIDR notation into a list of IPv4 addresses.|
|array ipv6ToCidr($ipFrom, $ipTo)||Convert IPv6 range into a list of IPv6 CIDR notation.|
|array cidrToIpv6($cidr)||Convert IPv6 CIDR notation into a list of IPv6 addresses.|
|string compressIpv6($ipv6)||Compress a IPv6 to shorten the length.|
|string expandIpv6($ipv6)||Expand a shorten IPv6 to full length.|
|string getVisitorIp()||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.|
Below is the description of the functions available in the Country class.
|Constructor||Expect a IP2Location Country Information CSV file. This database is free for download at https://www.ip2location.com/free/country-information|
|array 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. Below is the information returned:
Below is the description of the functions available in the Region class.
|Constructor||Expect a IP2Location ISO 3166-2 Subdivision Code CSV file. This database is free for download at https://www.ip2location.com/free/iso3166-2|
|string getRegionCode($countryCode, $regionName)||Provide a ISO 3166 country code and the region name to get ISO 3166-2 subdivision code for the region.|
This library requires IP2Location BIN data file to function. You may download the BIN data file at
- IP2Location LITE BIN Data (Free): https://lite.ip2location.com
- IP2Location Commercial BIN Data (Comprehensive): https://www.ip2location.com
An outdated BIN database was provided in the databases folder for your testing. You are recommended to visit the above links to download the latest BIN database.
You can also sign up for IP2Location Web Service to lookup by IP2Location API.
- Use the IPv4 BIN file if you just need to query IPv4 addresses.
- Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.
Below are the list of other framework library that you can install and use right away.
- IP2Location Laravel
- IP2Location CakePHP
- IP2Location CodeIgniter
- IP2Location Yii
- Symfony Framework. Tutorial on the Symfony implementation.
Copyright (C) 2005-2022 by IP2Location.com
License under MIT