PHP Projects – Free IP Geolocation API with IPinfoDB

IPinfoDB offers a free IP Geography API that is compatible with basic PHP. This allows you to try to determine location of the cloud servers that you use for your organization.

Remember that is basically just a Database query so results should be taken with an amount of objectivity.

IPinfoDB limits the api calls to 2 per second.

Requirements:

  • IPinfoDB API Key
  • Linux Server with PHP and Apache(or other webserver)

IPinfoDB: https://www.ipinfodb.com

iplocation.php

<h2>Where's The Server?</h2>
<form action ='iplocation.php' method='post'>
Domain <input type='text' name='domain'>
<input type='submit'>    
</form>


<?php

require 'class.IPInfoDB.php';

//Get Domain from Form
$domain = $_POST['domain'];

//If Domain is NOT Empty Print Results
if($domain !=''){

    $result = shell_exec('ping -c 1 '.$domain);

    //Extract IP Address from Shell Exec Results
    preg_match('/'.$domain.'.*.bytes/',$result,$ipAddress);
    $ipAddress = implode(' ',$ipAddress);
    $ipAddress = explode(' ',$ipAddress);
    $ip = $ipAddress['1'];
    $ip = ltrim($ip,'(');
    $ip = rtrim($ip,')');

    echo "Domain: ".$domain."<br>";
    echo "IP Address: ".$ip."<br>";

    $ipinfodb = new IPInfoDB('YOUR_API_KEY');

    // Get country information only (Faster)
    $result = $ipinfodb->getCountry($ip);

    echo "<br>";
    echo 'Country Code : ' . $result['countryCode'] . '<br>';
    echo 'Country Name : ' . $result['countryName'] . '<br>';

    // Get city information (Slower)
    $result = $ipinfodb->getCity($ip);

    echo 'Country Code : ' . $result['countryCode'] . '<br>';
    echo 'Country Name : ' . $result['countryName'] . '<br>';
    echo 'Region Name  : ' . $result['regionName'] . '<br>';
    echo 'City Name    : ' . $result['cityName'] . '<br>';
    echo 'ZIP Code     : ' . $result['zipCode'] . '<br>';
    echo 'Latitude     : ' . $result['latitude'] . '<br>';
    echo 'Longitude    : ' . $result['longitude'] . '<br>';
    echo 'Time Zone    : ' . $result['timeZone'] . '<br>';

    //echo "<br><strong>Var Dump</strong><br>";
    //var_dump($result);
}

?>

class.IPInfoDB.php

<?php

class IPInfoDB
{
	protected $apiKey;

	public function __construct($apiKey)
	{
		if (!preg_match('/^[0-9a-z]{64}$/', $apiKey)) {
			throw exception('Invalid IPInfoDB API key.');
		}

		$this->apiKey = $apiKey;
	}

	public function getCountry($ip)
	{
		$response = @file_get_contents('http://api.ipinfodb.com/v3/ip-country?key=' . $this->apiKey . '&ip=' . $ip . '&format=json');

		if (($json = json_decode($response, true)) === null) {
			$json['statusCode'] = 'ERROR';

			return false;
		}

		$json['statusCode'] = 'OK';

		return $json;
	}

	public function getCity($ip)
	{
		$response = @file_get_contents('http://api.ipinfodb.com/v3/ip-city?key=' . $this->apiKey . '&ip=' . $ip . '&format=json');

		if (($json = json_decode($response, true)) === null) {
			$json['statusCode'] = 'ERROR';

			return false;
		}

		$json['statusCode'] = 'OK';

		return $json;
	}
}

Be the first to comment

Leave a Reply