Cody Raspien
Cody Raspien

Reputation: 1845

How to obtain location from ipinfo.io in PHP?

I am using ipinfo.io to get my current city (location) using PHP.

However, I am not able to see my city when using this piece of code.

$ipaddress = $_SERVER["REMOTE_ADDR"];

function ip_details($ip) {
    $json = file_get_contents("http://ipinfo.io/{$ip}/geo");
    $details = json_decode($json);
    return $details;
}

$details = ip_details($ipaddress);
echo $details->city;

I don't know where the error is.

Upvotes: 6

Views: 19393

Answers (3)

FrancisBFTC
FrancisBFTC

Reputation: 11

I know this post is old, but for people currently looking for this same question, this will solve the problem if it's on Localhost:

$ipaddress = $_SERVER["SERVER_ADDR"];

  function ip_details($ip) {
     $json = file_get_contents("http://ipinfo.io/");
     $details = json_decode($json); // decode json with geolocalization information
     return $details;
  }

 $details = ip_details($ipaddress);
 echo $details->ip; // Use city, ip or other thing... see https://ipinfo.io

If it is on Web Server Only use this:

$server = $_SERVER['REMOTE_ADDR']; //in localhost this is ::1
echo "Your IP is: ".$server;

Note: The 2 codes will get the public ip, the 1st code gets the public ip by Localhost and the 2nd code gets the public ip by the web server. :)

Upvotes: 1

Jacek Kowalewski
Jacek Kowalewski

Reputation: 2851

Are you working on localhost? Try the following code:

$ipaddress = $_SERVER["REMOTE_ADDR"];

function ip_details($ip) {
    $json = file_get_contents("http://ipinfo.io/{$ip}/geo");
    $details = json_decode($json); // HERE!!!
    return $details;
}

$details = ip_details($ipaddress);
echo $details->ip; // CHANGE TO IP!!!

If it returns Your IP, everything is OK, Your IP is probably 127.0.0.1, and this site does not know the location, so $details->city is not set. You must check if (isset($details->city)) and make an alternative script if the city is not there.


I see You still got problems. Try to do something like this:

$string = file_get_contents('http://ipinfo.io/8.8.8.8/geo');
var_dump($string);
$ipaddress = $_SERVER["REMOTE_ADDR"];
var_dump($ipaddress); 
$string2 = file_get_contents('http://ipinfo.io/'.$ipaddress.'/geo');
var_dump($string2);

And write in comments which one failed ;).


If only IP part is OK, try to read this one: File_get_contents not working?

And also run this code with maximum error reporting:

error_reporting(-1);

Before this part of code.

Upvotes: 2

Octal
Octal

Reputation: 420

function getClientIP(){
  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
  } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}

$ipaddress = getClientIP();

function ip_details($ip) {
  $json = file_get_contents("http://ipinfo.io/{$ip}/geo");
  $details = json_decode($json, true);
  return $details;
}

$details = ip_details($ipaddress);
echo $details['city'];

this should work.

however, I recommend you to get used to use curl instead of file_get_contents(), if you want a online resource. https://stackoverflow.com/a/5522668/3160141

Upvotes: 5

Related Questions