Skip to content

Instantly share code, notes, and snippets.

@joomlapro
Created August 7, 2012 23:11
Show Gist options
  • Save joomlapro/3290396 to your computer and use it in GitHub Desktop.
Save joomlapro/3290396 to your computer and use it in GitHub Desktop.
Get Check IP
<?php
$ip = '189.114.10.215';
var_dump(geoCheckIP($ip));
// Get an array with geoip-infodata
function geoCheckIP($ip)
{
// Check, if the provided ip is valid
if (!filter_var($ip, FILTER_VALIDATE_IP))
{
throw new InvalidArgumentException("IP is not valid");
}
// Contact ip-server
$response = @file_get_contents('http://www.netip.de/search?query=' . $ip);
if (empty($response))
{
throw new InvalidArgumentException("Error contacting Geo-IP-Server");
}
// Array containing all regex-patterns necessary to extract ip-geoinfo from page
$patterns = array();
$patterns["domain"] = '#Domain: (.*?)&nbsp;#i';
$patterns["country"] = '#Country: (.*?)&nbsp;#i';
$patterns["state"] = '#State/Region: (.*?)<br#i';
$patterns["town"] = '#City: (.*?)<br#i';
// Array where results will be stored
$ipInfo = array();
// Check response from ipserver for above patterns
foreach ($patterns as $key => $pattern)
{
// Store the result in array
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}
return $ipInfo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment