I had a look at my installation of Mint (which collects statistics) at one of the plugins called Geomint (which is a free plugin) that does just what you're looking for using hostip.info:
Code: Select all
/**************************************************************************
queryHostIP($ip_adr)
This is the function which queries HostIP.info and stores the data in our
local db
**************************************************************************/
function queryHostIP($ip_adr) {
$ip_adr_ip = long2ip($ip_adr);
$tbl_Prefix = $this->Mint->db['tblPrefix'];
$useCURL = in_array('curl', get_loaded_extensions());
$host = 'api.hostip.info';
$gateway = '/get_html.php';
$post = 'ip='.$ip_adr_ip.'&position=true';
$data = '';
if ($useCURL) {
//Use cURL
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, "http://{$host}{$gateway}");
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $post);
$data = curl_exec($handle);
if (curl_errno($handle))
{
$error = 'Could not connect to Gateway (using cURL): '.curl_error($handle);
}
curl_close($handle);
}
else
{
//Use fopen
$handle = fopen("http://{$host}{$gateway}?{$post}", 'rb'); // open connection to hostip.info
while (!feof($handle)) {
$data .= fread($handle, 8192); // getting the data
}
fclose($handle);
}
global $country, $country_abrv, $city, $lat, $long; // defining global vars. BETTER SOLUTION?
$pos = strpos($data, "City:");
$tmp_string = substr($data, 0, $pos);
$tmp_string = trim($tmp_string);
$tmp_string = substr($tmp_string, 9, strlen($tmp_string));
$country = substr($tmp_string, 0, strpos($tmp_string, "("));
$country = mysql_escape_string($country);
$country_abrv = substr($tmp_string, strpos($tmp_string, "("), strlen($tmp_string));
$country_abrv = trim($country_abrv, "()");
$tmp_string = substr($data, $pos, strpos($data, "Latitude:") - $pos);
$city = substr($tmp_string, 5, strlen($tmp_string));
$city = trim($city);
$city = mysql_escape_string($city);
$tmp_string = substr($data, strpos($data, "Latitude:"), strlen($data));
$lat = substr($tmp_string, 9, strpos($tmp_string, "Longitude:") - 9);
$lat = trim($lat);
$tmp_string = substr($tmp_string, strpos($tmp_string, "Longitude:"), strlen($tmp_string));
$long = substr($tmp_string, 10, strlen($tmp_string));
$long = trim($long);
if ($lat == NULL) { // if no geodata is present, set to zero. MAYBE NULL?
$lat = 0;
}
if ($long == NULL) { // same here
$long = 0;
}
$query_hostip = "INSERT INTO {$tbl_Prefix}geo (ip, country, country_abrv, city, latitude, longitude)
VALUES ('$ip_adr', '$country', '$country_abrv', '$city', $lat, $long)"; // storing data in db
$result_hostip = $this->query($query_hostip); // do the query, baby!
return;
}