Getting latitude, longitude, timezone using ip address.
This is a post to find the latitude, longitude, timezone, country, city and so on using the web services/api by ipinfodb. The ipinfodb provides api in both xml and json format. We can use either of them. I will show you how to use it for both formats.
Using XML format:
You can either register for api key from ipinfodb. I am using mine in this example.
Output
Using JSON format
Output
Using XML format:
You can either register for api key from ipinfodb. I am using mine in this example.
$key="9dcde915a1a065fbaf14165f00fcc0461b8d0a6b43889614e8acdb8343e2cf15";
$ip= "208.115.219.10";
$ip= "208.115.219.10";
$url = "http://api.ipinfodb.com/v3/ip-city/?key=$key&ip=$ip&format=xml";
// load xml file
$xml = simplexml_load_file($url);
// print the name of the first element
echo $xml->getName() . "<br />";
// create a loop to print the element name and data for each node
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
$xml = simplexml_load_file($url);
// print the name of the first element
echo $xml->getName() . "<br />";
// create a loop to print the element name and data for each node
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
Output
Response
statusCode: OK
statusMessage:
ipAddress: 208.115.219.10
countryCode: US
countryName: UNITED STATES
regionName: NEW JERSEY
cityName: FLEMINGTON
zipCode: 08822
latitude: 40.5207
longitude: -74.8671
timeZone: -05:00
statusCode: OK
statusMessage:
ipAddress: 208.115.219.10
countryCode: US
countryName: UNITED STATES
regionName: NEW JERSEY
cityName: FLEMINGTON
zipCode: 08822
latitude: 40.5207
longitude: -74.8671
timeZone: -05:00
Using JSON format
$key="9dcde915a1a065fbaf14165f00fcc0461b8d0a6b43889614e8acdb8343e2cf15";
$ip=$_POST['address'];
$ip=$_POST['address'];
$url = "http://api.ipinfodb.com/v3/ip-city/?key=$key&ip=$ip&format=json";
$cont = file_get_contents($url);
$data = json_decode($cont , true);
$data = json_decode($cont , true);
if(strlen($data['latitude']))
{
$result = array(
'ip' => $data['ipAddress'] ,
'country_code' => $data['countryCode'] ,
'country_name' => $data['countryName'] ,
'region_name' => $data['regionName'] ,
'city' => $data['cityName'] ,
'zip_code' => $data['zipCode'] ,
'latitude' => $data['latitude'] ,
'longitude' => $data['longitude'] ,
'time_zone' => $data['timeZone'] ,
);
}
print_r($result);
{
$result = array(
'ip' => $data['ipAddress'] ,
'country_code' => $data['countryCode'] ,
'country_name' => $data['countryName'] ,
'region_name' => $data['regionName'] ,
'city' => $data['cityName'] ,
'zip_code' => $data['zipCode'] ,
'latitude' => $data['latitude'] ,
'longitude' => $data['longitude'] ,
'time_zone' => $data['timeZone'] ,
);
}
print_r($result);
Output
Array
(
[ip] => 208.115.219.10
[country_code] => US
[country_name] => UNITED STATES
[region_name] => NEW JERSEY
[city] => FLEMINGTON
[zip_code] => 08822
[latitude] => 40.5207
[longitude] => -74.8671
[time_zone] => -05:00
)
Is it free api key Can i use this forever without any restrictions.
ReplyDeleteIs it free api key Can i use this forever without any restrictions.
ReplyDelete