Advertisement
reenadak

get location via ip

Feb 20th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. /*
  2.     - Get Location (ip-api)
  3.     - Sends a request to ip-api via cURL to get the approximate location of the specified IP
  4.    
  5.     $format     - What the response should look like:
  6.                   0 => Town/City, County/State, Country
  7.                   1 => Town/City
  8.                   2 => County/State
  9.                   3 => Town/City, County/State
  10.                   4 => Country
  11.                   5 => State Code (ie NY)
  12.                   6 => Town/City, County/State, State Code, Country
  13.                   7 => Array of values
  14.                        => [0] = success or failure
  15.                        => [1] = Country
  16.                        => [2] = Country Code ie. GB
  17.                        => [3] = State Code ie. NY
  18.                        => [4] = Country/County/State
  19.                        => [5] = Town/City
  20.                        => [6] = Post/ZIP Code
  21.                        => [7] = Longitude
  22.                        => [8] = Latitude
  23.                        => [9] = Timezone ie. Europe/London
  24.                        => [10] = ISP
  25.                        => [11] = ISP
  26.                        => [12] = ISP Full Name
  27.                        => [13] = The IP address
  28.    
  29.     $ip       - The IP to get the location of
  30.     $timeout  - Number of seconds to wait before giving up
  31.     */
  32.    
  33.     public function getLocation($format = 0, $ip = '', $timeout = 5) {
  34.         if ($ip == '')
  35.             $ip = $_SERVER['REMOTE_ADDR'];
  36.        
  37.         $ch = curl_init();
  38.        
  39.         curl_setopt($ch, CURLOPT_URL, "http://ip-api.com/csv/" . $ip);
  40.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  41.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  42.        
  43.         $data = curl_exec($ch);
  44.         curl_close($ch);
  45.        
  46.         $arr = explode(",", $data);
  47.        
  48.         if ($arr[0] == "success") {
  49.             // remove speech marks
  50.             $i = 0;
  51.             foreach ($arr as $ari) {
  52.                 $arr[$i] = str_replace('"', '', $ari);
  53.                 $i++;
  54.             }          
  55.            
  56.             if ($format == 0) {
  57.                 return $arr[5] . ', ' . $arr[4] . ', ' . $arr[1];  
  58.             } elseif ($format == 1) {
  59.                 return $arr[5];
  60.             } elseif ($format == 2) {
  61.                 return $arr[4];
  62.             } elseif ($format == 3) {
  63.                 return $arr[5] . ', ' . $arr[4];
  64.             } elseif ($format == 4) {
  65.                 return $arr[1];
  66.             } elseif ($format == 5) {
  67.                 return $arr[3];
  68.             } elseif ($format == 6) {
  69.                 return $arr[5] . ', ' . $arr[4] . ', ' . $arr[3] . ', ' . $arr[1];
  70.             } else {
  71.                 return $arr;
  72.             }
  73.         } else {
  74.             return false;
  75.         }
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement