Importing IP2Location data into DynamoDB and querying with PHP (IPv6)

The aim of this guide is to demonstrate how to import IP2Location data (DB1) 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 DB1 csv file.
Download Free LITE version at http://lite.ip2location.com/database-ip-country
Download commercial version at http://ip2location.com/download?code=DB1IPV6

Extract out the IPV6-COUNTRY.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

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 config.php and paste the following code into it:

<?php
return array(
	'includes' => array('_aws'),
	'services' => array(
		'default_settings' => array(
			'params' => array(
				'key'    => 'YOUR_KEY_ID',
				'secret' => 'YOUR_SECRET_KEY',
				'region' => 'YOUR_REGION'
			)
		)
	)
);
?>

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

<?php
require('./aws.phar');
use Aws\DynamoDb\DynamoDbClient;
use Aws\Common\Enum\Region;
use Aws\DynamoDb\Enum\KeyType;
use Aws\DynamoDb\Enum\Type;

$db = 'DB1';
$filename = 'IPV6-COUNTRY.CSV';

$itemsperbatch = 25; // limit of bulk import API
$padzero = 40; // length to pad with zeroes

// Instantiate the client with your AWS credentials
$aws = Aws\Common\Aws::factory('./config.php');
$client = $aws->get("dynamodb");

do {
	$response = $client->listTables(array('Limit' => 2, 'ExclusiveStartTableName' => isset($response) ? $response['LastEvaluatedTableName'] : null)); 
	
	foreach ($response['TableNames'] as $key => $value) {
		if ($value == $db) // DB1 already exists so must drop first
		{
			// drop all DB1
			$result = $client->deleteTable(array("TableName" => $db));
			$client->waitUntilTableNotExists(array("TableName" => $db));
		}
	}
}
while ($response['LastEvaluatedTableName']);

$attributes = array();
$keys = array();

$attributes[] = array("AttributeName" => "MY_ID", "AttributeType" => Type::STRING);
$attributes[] = array("AttributeName" => "IP_TO", "AttributeType" => Type::STRING);
$keys[] = array("AttributeName" => "MY_ID", "KeyType" => KeyType::HASH);
$keys[] = array("AttributeName" => "IP_TO", "KeyType" => KeyType::RANGE);

// create new DB1
$tablearr = array("TableName" => $db, "AttributeDefinitions" => $attributes, "KeySchema" => $keys, "ProvisionedThroughput" => array("ReadCapacityUnits" => 5, "WriteCapacityUnits" => 25));

$result = $client->createTable($tablearr);
$client->waitUntilTableExists(array("TableName" => $db));

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

function doImport($dataarr) {
	global $client;
	global $db;
	global $counter;
	global $padzero;
	
	$mainarr = array();
	foreach ($dataarr as $data) {
		$data = rtrim($data); // clear EOL
		
		if (preg_match('/^"[^"]+","([^"]+)","([^"]+)","([^"]+)"$/', $data, $matches) == 1) {
			$itemarr = array();
			$attributearr = array();
			
			$counter++;
			$ipto = $matches[1];
			$attributearr["MY_ID"] = array(Type::STRING => $db); // DynamoDB needs 2 different field names for hash key and range key
			$attributearr["IP_TO"] = array(Type::STRING => str_pad($ipto, $padzero, '0', STR_PAD_LEFT));
			$countrycode = $matches[2];
			$attributearr["COUNTRY_CODE"] = array(Type::STRING => $countrycode);
			$countryname = $matches[3];
			$attributearr["COUNTRY_NAME"] = array(Type::STRING => $countryname);
			
			$itemarr = array("PutRequest" => array("Item" => $attributearr));
			$mainarr[] = $itemarr;
		}
	}
	
	$finalarr = array('RequestItems' => array($db => $mainarr));
	redo:
	$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 = array('RequestItems' => $result['UnprocessedItems']);
		goto redo;
	}
}
?>

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');

use Aws\DynamoDb\DynamoDbClient;
use Aws\Common\Enum\Region;
use Aws\DynamoDb\Enum\Type;
use Aws\DynamoDb\Enum\ComparisonOperator;

$db = 'DB1';

//test IP
$ip = '8.8.8.8';

// Instantiate the client with your AWS access keys
$aws = Aws\Common\Aws::factory("./config.php");
$client = $aws->get("dynamodb");

function ip62long($ipv6) {
	$ip_n = inet_pton($ipv6);
	$bits = 15;
	$ipv6long = 0;
 
	while($bits >= 0) {
		$bin = sprintf('%08b',(ord($ip_n[$bits])));
 
		if($ipv6long){
			$ipv6long = $bin . $ipv6long;
		}
		else{
			$ipv6long = $bin;
		}
		$bits--;
	}
	return gmp_strval(gmp_init($ipv6long, 2), 10);
}

function queryIP2Location($myip) {
	global $db;
	global $client;
	
	$padzero = 40; // need to pad the ip numbers because DynamoDB is comparing as strings, not numbers
	
	// convert IP address to IP number
	if (filter_var($myip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
		$myip = '::FFFF:' . $myip;
	}
	if (filter_var($myip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
		$ipnum = ip62long($myip);
	}
	
	// pad ipnum to 40 digits with zeroes in front so we can do string comparison
	$ipnum = str_pad($ipnum, $padzero, '0', STR_PAD_LEFT);
	
	$request = array(
		"TableName" => $db,
		"KeyConditions" => array(
			"MY_ID" => array(
				"ComparisonOperator" => ComparisonOperator::EQ,
				"AttributeValueList" => array(
					array(Type::STRING => $db)
				)
			),
			"IP_TO" => array(
				"ComparisonOperator" => ComparisonOperator::GE,
				"AttributeValueList" => array(
					array(Type::STRING => $ipnum)
				)
			)
		),
		"AttributesToGet" => array("COUNTRY_CODE", "COUNTRY_NAME"),
		"ConsistentRead" => true,
		"Limit" => 1
	);
	
	$response = $client->query($request);
	
	$result = array();
	
	foreach ($response['Items'] as $key => $value) {
		$result["COUNTRY_CODE"] = $value["COUNTRY_CODE"]["S"];
		$result["COUNTRY_NAME"] = $value["COUNTRY_NAME"]["S"];
	}
	
	return $result;
}

$myresult = queryIP2Location($ip);

echo 'COUNTRY_CODE: ' . $myresult["COUNTRY_CODE"] . "<br>\n";
echo 'COUNTRY_NAME: ' . $myresult["COUNTRY_NAME"] . "<br>\n";

?>

Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.