Return to Snippet

Revision: 65489
at December 6, 2013 21:36 by Hexahow


Initial Code
// description of your code here
/********************************************************************************/
/* Description: Get Longitude and Latitude by IP address in PHP */
/* For information, please visit http://www.ip2location.com */
/********************************************************************************/

<?php

$api_key = "insert api key here";
$ip_addr = get_remote_ip_address();
//Only IP2Location Web Service package WS5, WS9 and WS10 contains latitude and longitude information
$package = "insert package number";

$service_url = "http://api.ip2location.com/?ip=$ip_addr&key=$api_key&package=$package";

//Setting up connection using the service url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

$temp = $result;
$result_split = explode(";", $temp);
//Retrieving latitude and longitude information from the result returned by the API
$latitude = $result_split[4];
$longitude = $result_split[5];

echo $temp . "<br />";
echo "latitude : " . $latitude . "<br />";
echo "longitude : " . $longitude . "<br />";

function get_remote_ip_address()
{
   // Check to see if an HTTP_X_FORWARDED_FOR header is present.

   if($_SERVER['HTTP_X_FORWARDED_FOR'])

   {
   
      // If the header is present, use the last IP address.
      $temp_array      = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
      $temp_ip_address = $temp_array[count($temp_array) - 1];  
   }
   else
   {
      // If the header is not present, use the 
      // default server variable for remote address.
      $temp_ip_address = $_SERVER['REMOTE_ADDR'];
   }

   return $temp_ip_address;
}
?>

Initial URL
http://www.ip2location.com

Initial Description
// description of your code here
/********************************************************************************/
/* Description: Get Longitude and Latitude by IP address in PHP */
/* For information, please visit http://www.ip2location.com */
/********************************************************************************/

Initial Title
Get Longitude and Latitude by IP address in PHP

Initial Tags

                                

Initial Language
PHP