0

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");
imgg
  • 430
  • 6
  • 12

2 Answers2

0

Try this

function ip_info($ip = NULL, $deep_detect = TRUE) {
    if ($ip == NULL || !filter_var($ip, FILTER_VALIDATE_IP)) {
        $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'];
            }
        }
    }

    $ch = curl_init("http://ip-api.com/json/".$ip);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $result = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($result, true);

    if($result['status'] == "fail"){
        $result = FALSE; /* If an Error has come back, you can use $result['message'] to get why. */
    } 
    else { $result =  $result['countryCode']; } // If Sucess
    return $result;
}
Noah
  • 859
  • 7
  • 17
  • The API you were using was slow and also using file_get_contents is not a good idea. – Noah Dec 17 '17 at 02:22
  • Not a problem Have fun with it! Check the API URL and see what other things you can get. http://ip-api.com/json/ – Noah Dec 17 '17 at 02:37
  • @imgg we had an outage yesterday morning which might'be been why you got this. I'm able to run the code in the example from the question you linked to and the php test code on our docs page ipdata.co/docs.html – Jonathan Dec 18 '17 at 13:59
0

We had an outage yesterday morning that might've caused this. I've tested the following code and it works for me. Let me know if you have any issues.

function ip_info($ip = NULL, $deep_detect = TRUE) {
    if ($ip == NULL || !filter_var($ip, FILTER_VALIDATE_IP)) {
        $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'];
            }
        }
    }

    $ch = curl_init("https://api.ipdata.co/".$ip);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $result = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($result, true);

    $result =  $result['country_code'];
    return $result;
}
var_dump(ip_info('1.1.1.1'));
//string(2) "AU"
Jonathan
  • 10,792
  • 5
  • 65
  • 85
  • 1
    interesting...! never mind, i have been able to use the ipdata now. thanks for replying! – imgg Dec 26 '17 at 06:01