Skip to content

Instantly share code, notes, and snippets.

@lideo
Created December 18, 2014 18:28
Show Gist options
  • Save lideo/c5d43fa4c710d19728c3 to your computer and use it in GitHub Desktop.
Save lideo/c5d43fa4c710d19728c3 to your computer and use it in GitHub Desktop.
IP2Country
<?php
$service_url = 'http://ipinfo.io/8.8.8.8/json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
echo '<pre>';
print_r($decoded->country);
@coderholic
Copy link

If you just want the country details add /country to the http://ipinfo.io URL instead of /json. It'll be quicker, and save you from having to parse JSON. See http://ipinfo.io/developers for more details).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment