Skip to content

Wait till I come! » Finding the current location by IP and with the W3C Geo API

Link To Full Story: www.wait-till-i.com

Here’s how this works:

You can guess the location of a user by their IP and Rasmus Lerdorf wrote a nice API to do that at http://geoip.pidgets.com/. Using that, you can read the IP in PHP and call the API with cURL:

if ($_SERVER['HTTP_X_FORWARD_FOR']) {
    $ip = $_SERVER['HTTP_X_FORWARD_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
  $url = 'http://geoip.pidgets.com/?ip='.$ip.'&format=json';
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $output = curl_exec($ch); 
  curl_close($ch);
  $data = json_decode($output);
  $lat = $data->latitude;
  $lon = $data->longitude;

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*