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 the protected $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)));
.
Simply using $data['country'] will not work because the given result is not an object or array. You need to split the content into an array or, even better, retrieve it as a JSON object and then use:
$data = json_decode($data);
To give you the data in an array.
Despite the above, I wouldn't advise getting locations from IP Addresses because it is incredibly inaccurate! It returns the location of the Host server, not necessarily the location of the user.
EDIT:
Use the JSON API: http://ipinfodb.com/ip_location_api_json.php
Update:
Steps to implementing the JSON API:
Copy and past the following PHP code:
// Set the parameters
$ip_address = $_SERVER['REMOTE_ADDR'];
$api_key = 'YOUR_API_KEY_HERE';
// Get the data
$data = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=$api_key&ip=$ip_address&format=json");
// Decode the JSON result into an array
$data = json_decode($data);
// All data can now be accessed using the associative array $data
$country = $data['Country']; // Country
$city = $data['City']; // City
您可以使用换行分隔符将其拆分为一个数组,然后使用一系列字符串操纵符对其进行清理...或者您可以找到一个将数据返回为json的服务,这将完全缓解所有这些麻烦。
you can make an array like this :
array(
array(
'ip' => '0.0.0.0',
'country' => 'someplace',
'city' => 'somecity'm ,
'data' => 'somedata'),
array(
'ip' => '0.0.0.0',
'country' => 'someplace',
'city' => 'somecity'm ,
'data' => 'somedata'),
.
.
. );
and then serialize the array and save it via ip address in database and then you just have 2 table column :
ip ::: info
then you can use a function to retrieve data from db and universalize the array retrieved from info column.more information about php serialze
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.