RSS

Geocode IP Address with popular web services

04 Jul

hostip.info

using json API

$pageContent = file_get_contents('http://api.hostip.info/get_json.php?ip=70.85.226.228' );
$parsedJson  = json_decode($pageContent);
echo $parsedJson->country_name;
echo $parsedJson->city;
$ip = '203.153.223.84';
$sturl = 'http://api.hostip.info/get_html.php?ip='.$ip;
$ch = curl_init($sturl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$res = curl_exec($ch);
$resinfo = curl_getinfo($ch);
curl_close($ch);
preg_match('/City: ([a-zA-Z].+[a-zA-Z]+)/', $res, $r);
preg_match('/ \(([A-Z][A-Z])/', $res, $s);
$city = $r[1];
$country = $s[1];

echo $city;
echo $country;

freegeoip.net

$pageContent = file_get_contents('http://freegeoip.net/json/70.85.226.228' );
$parsedJson  = json_decode($pageContent);
echo $parsedJson->city;
echo $parsedJson->region_code;
echo $parsedJson->region_name;
echo $parsedJson->metrocode;
echo $parsedJson->zipcode;
echo $parsedJson->country_name;
echo $parsedJson->country_code;
echo $parsedJson->ip;
echo $parsedJson->latitude;
echo $parsedJson->longitude;

Geoips.com

<?php
function freegeoip_locate($ip) {
$url = "http://api.geoips.com/ip/".$ip."/key/<<your key>>/output/json/timezone/true/hostname/true";
$geo = json_decode(file_get_contents($url), true);
return $geo;
}

$geo = freegeoip_locate($_SERVER['REMOTE_ADDR']);

echo "ip: " . $geo['ip'];
echo "host name: ". $geo['hostname'];
echo "owner: ". $geo['owner'];
echo "continent_name: ". $geo['continent_name'];
echo "continent_code: ". $geo['continent_code'];
echo "country_name: ". $geo['country_name'];
echo "country_code: ". $geo['country_code'];
echo "region_name: ". $geo['region_name'];
echo "region_code: ". $geo['region_code'];
echo "county_name: ". $geo['county_name'];
echo "city_name: ". $geo['city_name'];
echo "latitude: ". $geo['latitude'];
echo "longitude: ". $geo['longitude'];
echo "timezone: ". $geo['timezone'];
?>
Advertisements
 
Leave a comment

Posted by on July 4, 2012 in PHP

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: