Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <pre><code>$data = json_decode($data); </code></pre> <p>To give you the data in an array.</p> <p><strong>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.</strong></p> <p><strong>EDIT:</strong></p> <p>Use the JSON API: <a href="http://ipinfodb.com/ip_location_api_json.php" rel="nofollow">http://ipinfodb.com/ip_location_api_json.php</a></p> <p><strong>Update:</strong></p> <p>Steps to implementing the JSON API:</p> <ol> <li>Register for an API key here: <a href="http://ipinfodb.com/register.php" rel="nofollow">http://ipinfodb.com/register.php</a>. This will allow you to retrieve results from their server, without this it will not work.</li> <li>You do not need to implement any SDK to get this to work, so all you should need is this:</li> </ol> <p>Copy and past the following PHP code:</p> <pre><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&amp;ip=$ip_address&amp;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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload