Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. class MaxMind
  5. {
  6.     const USERNAME = '125111';
  7.     const PASSWORD = 'LhIfO1JHWeNo';
  8.  
  9.     public function getLocation($user_ip)
  10.     {
  11.         $authenticationKey = sprintf('%s:%s', self::USERNAME, self::PASSWORD);
  12.         $authenticationKey = base64_encode($authenticationKey);
  13.         $httpOptions = [
  14.             'http' => [
  15.                 'method' => 'GET',
  16.                 'header' => 'Authorization: Basic ' . $authenticationKey . "\r\n"
  17.             ]
  18.         ];
  19.  
  20.         $context = stream_context_create($httpOptions);
  21.         //Получаем json объект данных об IP пользователя
  22.         $geo = file_get_contents("https://geoip.maxmind.com/geoip/v2.1/insights/$user_ip", false, $context);
  23.         $geo = json_decode($geo, true);
  24.         $user_location = [];
  25.  
  26.         //одним массивом все отправлять
  27.         /*
  28.             //$user_location = json_decode($geo, true);
  29.             foreach ($geo as $type => &$values) {
  30.                 foreach ($values as $key => &$subValues) {
  31.                     if ($key !== 'names') {
  32.                         continue;
  33.                     }
  34.                     $subValues = ['en' => $subValues['en']];
  35.                 }
  36.             }
  37.     */
  38.         //city
  39.         $user_location['city_confidence'] = $geo["city"]["confidence"];
  40.         $user_location['city_id'] = $geo["city"]["geoname_id"];
  41.         $user_location['city_name'] = $geo["city"]["names"]["en"];
  42.  
  43.         //continent
  44.         $user_location['continent_code'] = $geo["continent"]["code"];
  45.         $user_location['continent_id'] = $geo["continent"]["geoname_id"];
  46.         $user_location['continent_name'] = $geo["continent"]["names"]["en"];
  47.  
  48.         //country
  49.         $user_location['country_confidence'] = $geo["country"]["confidence"];
  50.         $user_location['country_iso_code'] = $geo["country"]["iso_code"];
  51.         $user_location['country_id'] = $geo["country"]["geoname_id"];
  52.         $user_location['country_name'] = $geo["continent"]["names"]["en"];
  53.  
  54.         //location
  55.         $user_location['location_radius'] = $geo["location"]["accuracy_radius"];
  56.         $user_location['latitude'] = $geo["location"]["latitude"];
  57.         $user_location['longitude'] = $geo["location"]["longitude"];
  58.         $user_location['time_zone'] = $geo["location"]["time_zone"];
  59.  
  60.         //maxmind
  61.         $user_location['maxmind'] = $geo["maxmind"]["queries_remaining"];
  62.  
  63.         //subdivision
  64.         $user_location['location_conf'] = $geo["subdivisions"][0]["confidence"];
  65.         $user_location['location_iso_code'] = $geo["subdivisions"][0]["iso_code"];
  66.         $user_location['location_id'] = $geo["subdivisions"][0]["geoname_id"];
  67.         $user_location['location_name'] = $geo["subdivisions"][0]["names"]["en"];
  68.  
  69.         //traits
  70.         $user_location['user_type'] = $geo["traits"]["user_type"];
  71.         $user_location['autonomous_system_number'] = $geo["traits"]["autonomous_system_number"];
  72.         $user_location['autonomous_system_organization'] = $geo["traits"]["autonomous_system_organization"];
  73.         $user_location['ip_addres'] = $geo["traits"]["ip_address"];
  74.  
  75.         return $user_location;
  76.     }
  77. }
  78.  
  79.  
  80. $user_ip = $_SERVER['REMOTE_ADDR'];
  81. $user_ip = '95.90.207.139';
  82. $obj = new MaxMind();
  83. $arr = $obj->getLocation($user_ip);
  84.  
  85.  
  86. var_dump($arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement