2

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.

4 Answers 4

5

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)));.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I tried that now, but it still isn't working... I am using the same code as above with the following changes: $iplookup = "http://api.ipinfodb.com/v3/ip-city/?key=<myapikey>&ip=$ip"; $data = json_decode(file_get_contents($iplookup)); $country = $data['CountryCode']; $city = $data['CityName']; but still I'm not getting the data. Only "O" and "O" for each
Ok, I tried that now, but it still isn't working... this is the code I am using: $data = var_export(unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$ip"))); $country = $data['geoplugin_countryCode']; $city = $data['geoplugin_city'];
This should work: $data = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=".$ip)); $country = $data['geoplugin_countryCode']; $city = $data['geoplugin_city']; (or at least it works for me).
1

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:

  1. Register for an API key here: http://ipinfodb.com/register.php. This will allow you to retrieve results from their server, without this it will not work.
  2. You do not need to implement any SDK to get this to work, so all you should need is this:

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 

5 Comments

I don't need the precise location, just the region, therefore I am using the ip for location for now
It is not even accurate for the region though. My IP came up as Manchester, I am over 200 miles away!
awesome! that is more than great for our purposes.
Okay, well let me know how you get on with the above
I just did it using geoplugin - see above
0

You can split into an array with a newline separator, and then scrub it with a series of string manipulators... or you can find a service that will return the data as json, which will totally ease the pain of going through all of that.

1 Comment

part a: can you show/link to sample code? part b: see my comment to axiomer above, I am getting json there, but still it isn't working...
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.