Created
December 18, 2014 18:28
-
-
Save lideo/c5d43fa4c710d19728c3 to your computer and use it in GitHub Desktop.
IP2Country
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).