I am trying to use hostip in order to save to DB user’s general area/ location, according to IP address. However I am not sure how to parse the data from the output I am receiving.
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$data = json_decode(file_get_contents("http://api.hostip.info/get_html.php?ip=$ip")); //<-not sure how
$country = $data['Country'];
$city = $data['City'];
?>
You can see here how I am receiving the data:
http://api.hostip.info/get_html.php?ip=00.0.00.00
Another option was to use the first answer here (using ipinfodb): Getting location details from IP in PHP, but it doesn’t even display any data, even when I try inserting my IP in the url, let alone parse the results, so I changed to the above.
So, according to ipinfodb, you will have to sign up for an API key. But they have an API class, which’s
getCountry($host)will get the country of the IP after you gave theprotected $apiKey = '';your API key. Or if you don’t want to use that, you can use their XML or their JSON API.UPDATE 1
Or, if that is too hard for you, then use explode on the newline separator (\n) at the other service. Or, perhaps use another service.
UPDATE 2
Seems that geoPlugin, mentioned in the question you linked does not require an API key. Their code to use is
echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)));.