PS: This code is modified from Getting visitors country from their IP
I try to use the ipdata service from https://api.ipdata.co/ but when I try to use PHP to decode the json, nothing happened. Even when I try to print the api response out. Can anyone help me lol..
The php code:
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
$output = NULL;
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_CLIENT_IP'];
} else {
echo "error [2]";
}
} else {
echo "error [1]";
}
$rdata = file_get_contents("http://api.ipdata.co/" . $ip);
echo $rdata;
$ipdat = @json_decode($rdata);
echo $ipdat;
$output = @$ipdat->country_code;
return $output;
}
echo ip_info("Visitor", "country");