PHP code example of ip2location / ip2location-codeigniter4

1. Go to this page and download the library: Download ip2location/ip2location-codeigniter4 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ip2location / ip2location-codeigniter4 example snippets


$routes->get('ip2location_test', 'IP2Location_test::index');

    // (optional) Define IP2Location database path. By default, the IP2LOCATION_DATABASE is pointed to *app/Libraries/IP2Location/IP2LOCATION-DB.BIN* if you choose not to change the original settings.
    define('IP2LOCATION_DATABASE', '/path/to/ip2location/database');

    $ipl = new IP2Location_lib();
    $countryCode = $ipl->getCountryCode('8.8.8.8');

    $countryCode = $ipl->getCountryCode($ip);
    $countryName = $ipl->getCountryName($ip);
    $regionName = $ipl->getRegionName($ip);
    $cityName = $ipl->getCityName($ip);
    $latitude = $ipl->getLatitude($ip);
    $longitude = $ipl->getLongitude($ip);
    $isp = $ipl->getISP($ip);
    $domainName = $ipl->getDomainName($ip);
    $zipCode = $ipl->getZIPCode($ip);
    $timeZone = $ipl->getTimeZone($ip);
    $netSpeed = $ipl->getNetSpeed($ip);
    $iddCode = $ipl->getIDDCode($ip);
    $areaCode = $ipl->getAreaCode($ip);
    $weatherStationCode = $ipl->getWeatherStationCode($ip);
    $weatherStationName = $ipl->getWeatherStationName($ip);
    $mcc = $ipl->getMCC($ip);
    $mnc = $ipl->getMNC($ip);
    $mobileCarrierName = $ipl->getMobileCarrierName($ip);
    $elevation = $ipl->getElevation($ip);
    $usageType = $ipl->getUsageType($ip);
    $addressType = $ipl->getAddressType($ip);
    $category = $ipl->getCategory($ip);

    // (ine('IP2LOCATION_API_KEY', 'your_api_key');

    // ( Define to use https or http.
    define('IP2LOCATION_USESSL', false);

    // (optional) Define extra information in addition to the above-selected package. Refer to https://www.ip2location.com/web-service/ip2location for the list of available addons.
    define('IP2LOCATION_ADDONS', []);

    // (optional) Define Translation information. Refer to https://www.ip2location.com/web-service/ip2location for available languages.
    define('IP2LOCATION_LANGUAGE', 'zh-cn');

    $ipl = new IP2Location_lib();
    print_r ($ipl->getWebService('8.8.8.8'));

	// (define('IP2LOCATION_IO_API_KEY', 'your_api_key');

    // (optional) Define Translation information. Refer to https://www.ip2location.io/ip2location-documentation for available languages.
    define('IP2LOCATION_IO_LANGUAGE', 'zh-cn');

    $ipl = new IP2Location_lib();
    print_r ($ipl->getWebService('8.8.8.8'));

    define('IP2LOCATION_DATABASE_TABLE', 'ip2location_table_name');

    $db = model('IP2Location_model', false);
    print_r ($db->lookup('8.8.8.8'));

    $ipl = new IP2Location_lib();
    var_dump($ipl->isIpv4('8.8.8.8'));echo '<br>';
    var_dump($ipl->isIpv6('2001:4860:4860::8888'));echo '<br>';
    print_r($ipl->ipv4ToDecimal('8.8.8.8'));echo '<br>';
    print_r($ipl->decimalToIpv4(134744072));echo '<br>';
    print_r($ipl->ipv6ToDecimal('2001:4860:4860::8888'));echo '<br>';
    print_r($ipl->decimalToIpv6('42541956123769884636017138956568135816'));echo '<br>';
    print_r($ipl->ipv4ToCidr('8.0.0.0', '8.255.255.255'));echo '<br>';
    print_r($ipl->cidrToIpv4('8.0.0.0/8'));echo '<br>';
    print_r($ipl->ipv6ToCidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff'));echo '<br>';
    print_r($ipl->cidrToIpv6('2002::1234:abcd:ffff:c0a8:101/64'));echo '<br>';
    print_r($ipl->compressIpv6('2002:0000:0000:1234:FFFF:FFFF:FFFF:FFFF'));echo '<br>';
    print_r($ipl->expandIpv6('2002::1234:FFFF:FFFF:FFFF:FFFF'));echo '<br>';