Importing IP2Location data into Redis and querying with PHP

The aim of this guide is to demonstrate how to import IP2Location data (DB25) in CSV form into Redis and then query the data in a PHP web page.

First of all, you will need to download the IP2Location DB25 CSV file.
Download commercial version at https://ip2location.com/download?code=DB25

Extract out the IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY.CSV file from the downloaded zipped file.

Important Note

We will not cover installation of Redis or PHP in this guide. We will assume you have already setup Redis on the localhost and are using PHP via Apache (also on the localhost). For this example, we are using a Debian Linux.

More info can be found at the following URLs if you need assistance with installations:
PHP: http://php.net/manual/en/install.unix.debian.php
Redis: http://redis.io/download
PHP Redis Library: http://pecl.php.net/package/redis

We will be using a Bash script to do mass import of CSV data into Redis and then querying the data via PHP using the PHP Redis library.

Importing the CSV data into Redis

As mentioned earlier, we will be using a Bash script to perform the import of the CSV data into Redis as we need to format our CSV data into an easier to store form.

Create a new file called importredis.sh and paste the following code into it:

#!/bin/bash

DB="DB25"
FILENAME="IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY.CSV"; # IP2Location DB25

# delete old DB first
echo "DEL "$DB | redis-cli --verbose

# transform and import data
cat $FILENAME | awk -vdb="$DB" 'BEGIN { FS="\",\""; } { $1 = substr($1, 2); $NF = substr($NF, 1, length($NF) - 1); printf "%s %s ", "ZADD", db; printf "%s \"%s|", $1, $1; for(i=3; i<=NF; i++) { if(i>3) { printf "%s", "|"; } printf "%s", $i; if(i==NF) { print ""; } } }' | redis-cli --pipe --pipe-timeout 0 --verbose

Run the Bash script by calling the below command in command prompt:
bash importredis.sh

Querying the IP2Location data from a PHP web page

Now, create a PHP file called test.php in your website.

Paste the following PHP code into it and then run it in the browser:

<?php
$ip = '8.8.8.8'; // test IP
 
function queryIP2Location($myip) {
    $redis = new Redis();
    $redis->connect('127.0.0.1'); // port 6379 by default
    if ($redis->auth('your_redis_password')) {
        // convert IP address to IP number
        $ipnum = sprintf("%u", ip2long($myip));
 
        $result = $redis->zRevRangeByScore('DB25', $ipnum, 0, array('limit' => array(0, 1)));
        $result = $result[0];
        $arr = explode("|", $result);
 
        echo "country_code: " . $arr[1] . "<br>\n";
        echo "country_name: " . $arr[2] . "<br>\n";
        echo "region_name: " . $arr[3] . "<br>\n";
        echo "city_name: " . $arr[4] . "<br>\n";
        echo "latitude: " . $arr[5] . "<br>\n";
        echo "longitude: " . $arr[6] . "<br>\n";
        echo "zip_code: " . $arr[7] . "<br>\n";
        echo "time_zone: " . $arr[8] . "<br>\n";
        echo "isp: " . $arr[9] . "<br>\n";
        echo "domain: " . $arr[10] . "<br>\n";
        echo "net_speed: " . $arr[11] . "<br>\n";
        echo "idd_code: " . $arr[12] . "<br>\n";
        echo "area_code: " . $arr[13] . "<br>\n";
        echo "weather_station_code: " . $arr[14] . "<br>\n";
        echo "weather_station_name: " . $arr[15] . "<br>\n";
        echo "mcc: " . $arr[16] . "<br>\n";
        echo "mnc: " . $arr[17] . "<br>\n";
        echo "mobile_brand: " . $arr[18] . "<br>\n";
        echo "elevation: " . $arr[19] . "<br>\n";
        echo "usage_type: " . $arr[20] . "<br>\n";
        echo "address_type: " . $arr[21] . "<br>\n";
        echo "category: " . $arr[22] . "<br>\n";
    }
    else {
        echo "Incorrect password\n";
    }
    $redis->close();
}
 
queryIP2Location($ip);
?>

Was this article helpful?

Related Articles