Importing IP2Location data into DynamoDB and querying with PHP

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

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

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

Also remember to download the AWS PHP SDK into the same folder as your web page.

Important Note

We will not cover installation of PHP in this guide. We will assume you have already setup PHP on the localhost and are using PHP via Apache (also on the localhost). For this example, we are using an Amazon EC2 instance running 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
AWS PHP SDK: http://docs.aws.amazon.com/aws-sdk-php/guide/latest/installation.html

Setup your AWS credentials by following: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_profiles.html

We will be using the AWS PHP SDK to do mass import of CSV data into DynamoDB and then querying the data via PHP using the same SDK.

Importing the CSV data into DynamoDB

Create a new PHP file called import.php and paste the following code into it:

<?php
require('./aws.phar');

$db = 'DB26';
$filename = 'IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.CSV';

$itemsperbatch = 25; // limit of bulk import API

date_default_timezone_set('UTC');

use Aws\DynamoDb\Exception\DynamoDbException;

$sdk = new Aws\Sdk([
	'region'   => 'YOUR_AWS_REGION',
	'version'  => 'latest'
]);

$client = $sdk->createDynamoDb();

do {
	$params['Limit'] = 2;
	if (isset($response)) {
		$params['ExclusiveStartTableName'] = $response['LastEvaluatedTableName'];
	}
	$response = $client->listTables($params); 
	
	foreach ($response['TableNames'] as $key => $value) {
		if ($value == $db) { // table already exists so must drop first
			echo "Deleting existing table\n";
			$result = $client->deleteTable(['TableName' => $db]);
		}
	}
}
while ($response['LastEvaluatedTableName']);

try {
	// waiting for existing table to be deleted
	do {
		$result = $client->describeTable(['TableName' => $db]);
		sleep(2);
	} while (true);
}
catch (DynamoDbException $e) {
	if (preg_match('/ResourceNotFoundException/', $e->getMessage()) !== 1) {
		die($e->getMessage() . "\n");
	}
}

$attributes = [];
$keys = [];

$attributes[] = ['AttributeName' => 'MY_ID', 'AttributeType' => 'S'];
$attributes[] = ['AttributeName' => 'IP_TO', 'AttributeType' => 'N'];
$keys[] = ['AttributeName' => 'MY_ID', 'KeyType' => 'HASH'];
$keys[] = ['AttributeName' => 'IP_TO', 'KeyType' => 'RANGE'];

$tablearr = ['TableName' => $db, 'AttributeDefinitions' => $attributes, 'KeySchema' => $keys, 'ProvisionedThroughput' => ['ReadCapacityUnits' => 5, 'WriteCapacityUnits' => 25]];

echo "Creating table\n";
$result = $client->createTable($tablearr);

redocheck:
try {
	do {
		// wait for table to become active
		$result = $client->describeTable(['TableName' => $db]);
		sleep(2);
	} while ($result['Table']['TableStatus'] != 'ACTIVE');
	echo "Table created\n";
}
catch (DynamoDbException $e) {
	if (preg_match('/ResourceNotFoundException/', $e->getMessage()) === 1) {
		// table not ready yet
		sleep(2);
		goto redocheck;
	}
	else {
		die($e->getMessage() . "\n");
	}
}

$handle = fopen($filename, 'r');
$contents = '';
$dataarr = [];
$counter = 0;
while (!feof($handle)) {
	$line = fgets($handle, 8192);
	$dataarr[] = $line;
	
	if (count($dataarr) == $itemsperbatch) {
		if ($counter % 5000 == 0) {
			echo 'Importing row ' . $counter . "\n";
		}
		doImport($dataarr);
		$dataarr = []; //reset
	}
}
fclose($handle);
if (count($dataarr) > 0) {
	doImport($dataarr);
	$dataarr = []; //reset
}

function doImport($dataarr) {
	global $client;
	global $db;
	global $counter;
	global $padzero;
	
	$mainarr = [];
	foreach ($dataarr as $data) {
		$data = rtrim($data); // clear EOL
		
		if (preg_match('/^"[^"]+","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)","([^"]+)"$/', $data, $matches) == 1) {
			$itemarr = [];
			$attributearr = [];
			
			$counter++;
			$ipto = $matches[1];
			$attributearr['MY_ID'] = ['S' => $db]; // DynamoDB needs 2 different field names for hash key and range key
			$attributearr['IP_TO'] = ['N' => $ipto];
			$countrycode = $matches[2];
			$attributearr['COUNTRY_CODE'] = ['S' => $countrycode];
			$countryname = $matches[3];
			$attributearr['COUNTRY_NAME'] = ['S' => $countryname];
			$regionname = $matches[4];
			$attributearr['REGION_NAME'] = ['S' => $regionname];
			$cityname = $matches[5];
			$attributearr['CITY_NAME'] = ['S' => $cityname];
			$lat = $matches[6];
			$attributearr['LATITUDE'] = ['S' => $lat];
			$long = $matches[7];
			$attributearr['LONGITUDE'] = ['S' => $long];
			$zipcode = $matches[8];
			$attributearr['ZIP_CODE'] = ['S' => $zipcode];
			$timezone = $matches[9];
			$attributearr['TIME_ZONE'] = ['S' => $timezone];
			$isp = $matches[10];
			$attributearr['ISP'] = ['S' => $isp];
			$domain = $matches[11];
			$attributearr['DOMAIN'] = ['S' => $domain];
			$netspeed = $matches[12];
			$attributearr['NET_SPEED'] = ['S' => $netspeed];
			$iddcode = $matches[13];
			$attributearr['IDD_CODE'] = ['S' => $iddcode];
			$areacode = $matches[14];
			$attributearr['AREA_CODE'] = ['S' => $areacode];
			$weatherstationcode = $matches[15];
			$attributearr['WEATHER_STATION_CODE'] = ['S' => $weatherstationcode];
			$weatherstationname = $matches[16];
			$attributearr['WEATHER_STATION_NAME'] = ['S' => $weatherstationname];
			$mcc = $matches[17];
			$attributearr['MCC'] = ['S' => $mcc];
			$mnc = $matches[18];
			$attributearr['MNC'] = ['S' => $mnc];
			$mobilebrand = $matches[19];
			$attributearr['MOBILE_BRAND'] = ['S' => $mobilebrand];
			$elevation = $matches[20];
			$attributearr['ELEVATION'] = ['S' => $elevation];
			$usagetype = $matches[21];
			$attributearr['USAGE_TYPE'] = ['S' => $usagetype];
			$addresstype = $matches[22];
			$attributearr['ADDRESS_TYPE'] = ['S' => $addresstype];
			$category = $matches[23];
			$attributearr['CATEGORY'] = ['S' => $category];
			$district = $matches[24];
			$attributearr['DISTRICT'] = ['S' => $district];
			$asn = $matches[25];
			$attributearr['ASN'] = ['S' => $asn];
			$as = $matches[26];
			$attributearr['AS'] = ['S' => $as];
			
			$itemarr = ['PutRequest' => ['Item' => $attributearr]];
			$mainarr[] = $itemarr;
		}
	}
	
	$finalarr = ['RequestItems' => [$db => $mainarr]];
	$retries = 1;
	dowrite:
	try {
		$result = $client->batchWriteItem($finalarr);
		
		// see if there are any items that couldn't be imported then we resubmit for import
		if (!empty($result['UnprocessedItems'])) {
			$finalarr = ['RequestItems' => $result['UnprocessedItems']];
			goto dowrite;
		}
	}
	catch (DynamoDbException $e) {
		if (preg_match('/(ProvisionedThroughputExceededException|Handshake timed out)/', $e->getMessage()) === 1) {
			sleep(2**$retries); // exponential retries
			echo "Retrying\n";
			$retries++;
			goto dowrite;
		}
		else {
			die($e->getMessage() . "\n");
		}
	}
}
?>

Run the PHP script by calling the below command in command prompt:
php import.php

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
require('./aws.phar');

$db = 'DB26';

date_default_timezone_set('UTC');

use Aws\DynamoDb\Exception\DynamoDbException;

$sdk = new Aws\Sdk([
	'region'   => 'YOUR_AWS_REGION',
	'version'  => 'latest'
]);

$client = $sdk->createDynamoDb();

function queryIP2Location($myip) {
	global $db;
	global $client;
	
	// convert IP address to IP number
	$ipnum = sprintf("%u", ip2long($myip));	
	
	$request = [
		'TableName' => $db,
		'KeyConditions' => [
			'MY_ID' => [
				'ComparisonOperator' => 'EQ',
				'AttributeValueList' => [
					['S' => $db]
				]
			],
			'IP_TO' => [
				'ComparisonOperator' => 'GT',
				'AttributeValueList' => [
					['N' => $ipnum]
				]
			]
		],
		'AttributesToGet' => ['COUNTRY_CODE', 'COUNTRY_NAME', 'REGION_NAME', 'CITY_NAME', 'LATITUDE', 'LONGITUDE', 'ZIP_CODE', 'TIME_ZONE', 'ISP', 'DOMAIN', 'NET_SPEED', 'IDD_CODE', 'AREA_CODE', 'WEATHER_STATION_CODE', 'WEATHER_STATION_NAME', 'MCC', 'MNC', 'MOBILE_BRAND', 'ELEVATION', 'USAGE_TYPE', 'ADDRESS_TYPE', 'CATEGORY', 'DISTRICT', 'ASN', 'AS'],
		'ConsistentRead' => true,
		'Limit' => 1
	];
	
	$response = $client->query($request);
	
	$result = [];
	
	foreach ($response['Items'] as $key => $value) {
		$result['COUNTRY_CODE'] = $value['COUNTRY_CODE']['S'];
		$result['COUNTRY_NAME'] = $value['COUNTRY_NAME']['S'];
		$result['REGION_NAME'] = $value['REGION_NAME']['S'];
		$result['CITY_NAME'] = $value['CITY_NAME']['S'];
		$result['LATITUDE'] = $value['LATITUDE']['S'];
		$result['LONGITUDE'] = $value['LONGITUDE']['S'];
		$result['ZIP_CODE'] = $value['ZIP_CODE']['S'];
		$result['TIME_ZONE'] = $value['TIME_ZONE']['S'];
		$result['ISP'] = $value['ISP']['S'];
		$result['DOMAIN'] = $value['DOMAIN']['S'];
		$result['NET_SPEED'] = $value['NET_SPEED']['S'];
		$result['IDD_CODE'] = $value['IDD_CODE']['S'];
		$result['AREA_CODE'] = $value['AREA_CODE']['S'];
		$result['WEATHER_STATION_CODE'] = $value['WEATHER_STATION_CODE']['S'];
		$result['WEATHER_STATION_NAME'] = $value['WEATHER_STATION_NAME']['S'];
		$result['MCC'] = $value['MCC']['S'];
		$result['MNC'] = $value['MNC']['S'];
		$result['MOBILE_BRAND'] = $value['MOBILE_BRAND']['S'];
		$result['ELEVATION'] = $value['ELEVATION']['S'];
		$result['USAGE_TYPE'] = $value['USAGE_TYPE']['S'];
		$result['ADDRESS_TYPE'] = $value['ADDRESS_TYPE']['S'];
		$result['CATEGORY'] = $value['CATEGORY']['S'];
		$result['DISTRICT'] = $value['DISTRICT']['S'];
		$result['ASN'] = $value['ASN']['S'];
		$result['AS'] = $value['AS']['S'];
	}
	
	return $result;
}

//test IP
$ip = '8.8.8.8';

$myresult = queryIP2Location($ip);
echo 'COUNTRY_CODE: ' . $myresult['COUNTRY_CODE'] . "<br>\n";
echo 'COUNTRY_NAME: ' . $myresult['COUNTRY_NAME'] . "<br>\n";
echo 'REGION_NAME: ' . $myresult['REGION_NAME'] . "<br>\n";
echo 'CITY_NAME: ' . $myresult['CITY_NAME'] . "<br>\n";
echo 'LATITUDE: ' . $myresult['LATITUDE'] . "<br>\n";
echo 'LONGITUDE: ' . $myresult['LONGITUDE'] . "<br>\n";
echo 'ZIP_CODE: ' . $myresult['ZIP_CODE'] . "<br>\n";
echo 'TIME_ZONE: ' . $myresult['TIME_ZONE'] . "<br>\n";
echo 'ISP: ' . $myresult['ISP'] . "<br>\n";
echo 'DOMAIN: ' . $myresult['DOMAIN'] . "<br>\n";
echo 'NET_SPEED: ' . $myresult['NET_SPEED'] . "<br>\n";
echo 'IDD_CODE: ' . $myresult['IDD_CODE'] . "<br>\n";
echo 'AREA_CODE: ' . $myresult['AREA_CODE'] . "<br>\n";
echo 'WEATHER_STATION_CODE: ' . $myresult['WEATHER_STATION_CODE'] . "<br>\n";
echo 'WEATHER_STATION_NAME: ' . $myresult['WEATHER_STATION_NAME'] . "<br>\n";
echo 'MCC: ' . $myresult['MCC'] . "<br>\n";
echo 'MNC: ' . $myresult['MNC'] . "<br>\n";
echo 'MOBILE_BRAND: ' . $myresult['MOBILE_BRAND'] . "<br>\n";
echo 'ELEVATION: ' . $myresult['ELEVATION'] . "<br>\n";
echo 'USAGE_TYPE: ' . $myresult['USAGE_TYPE'] . "<br>\n";
echo 'ADDRESS_TYPE: ' . $myresult['ADDRESS_TYPE'] . "<br>\n";
echo 'CATEGORY: ' . $myresult['CATEGORY'] . "<br>\n";
echo 'DISTRICT: ' . $myresult['DISTRICT'] . "<br>\n";
echo 'ASN: ' . $myresult['ASN'] . "<br>\n";
echo 'AS: ' . $myresult['AS'] . "<br>\n";
?>

Was this article helpful?

Related Articles