6

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Cody Raspien
  • 1,753
  • 5
  • 26
  • 51
  • 2
    And what is the error? What Have you tried? What are the logs? What does var_dump(j$json) output? PS. Thx for a great portal addrres for the future :). – Jacek Kowalewski Jan 18 '15 at 16:31
  • I tried to echo $json = file_get_contents('http://ipinfo.io/8.8.8.8/geo'); but there is nothing that appears - still with blank screen. – Cody Raspien Jan 18 '15 at 16:37
  • Try: var_dump(file_get_contents('http://ipinfo.io/8.8.8.8/geo')); If this does not show anything something is not ok with server configuration... Probably it blocks (how, and why?) getting contents from external IPs. Notice HTTP:// in my code. – Jacek Kowalewski Jan 18 '15 at 16:40
  • Tried to var dump - still blank. Do you think IPINFO is blocking me somehow? – Cody Raspien Jan 18 '15 at 16:42
  • Have You tried with http://? I updated my comment few seconds before. – Jacek Kowalewski Jan 18 '15 at 16:44
  • Off topic, but anyway: for my IP, the given estimated "City" is 350 km / 200 miles off from my real one. I don't know on what database they are working, but it's not __that__ precise ... – Axel Amthor Jan 18 '15 at 16:54
  • You need to specify the protocol (`http://`) when using file_get_contents to open an URL. And sometimes opening URLs through file_get_contents may be disabled, in which case you'll need to use curl or an HTTP client library. –  Jan 18 '15 at 16:59
  • @maytham my ISP is another 500 miles away :/ I live south of Munich, ipinfo claims me in Aalen, my ISP is in Düsseldorf and I have a static IP since 8 years. – Axel Amthor Jan 18 '15 at 18:25

3 Answers3

5
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

Community
  • 1
  • 1
Octal
  • 410
  • 1
  • 5
  • 12
  • the $ipaddress part is correct. I can get the ip address of client. However, the IPINFO part returns null - blank screen. – Cody Raspien Jan 18 '15 at 16:54
  • there was a mistake, I tipped json_dencode() instead a json_decode() – Octal Jan 18 '15 at 16:59
  • Still blank. I don't know what to do. – Cody Raspien Jan 18 '15 at 17:01
  • Found the issue - the Rackspace box I'm running this is shared and there are other scripts on that box which use ipinfo. The ip address of that box is blocked due to the limit being exceeded. – Cody Raspien Jan 18 '15 at 17:38
  • Worked like a charm, except I had to add the token - `http://ipinfo.io?token={your_account_token}/{$ip}/geo` Thanks! – Leo Apr 28 '17 at 19:56
2

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.

Community
  • 1
  • 1
Jacek Kowalewski
  • 2,761
  • 2
  • 23
  • 36
1

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. :)