SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | ||
4 | class MaxMind | |
5 | { | |
6 | const USERNAME = '125111'; | |
7 | - | public function getLocation($user_ip) |
7 | + | const PASSWORD = 'LhIfO1JHWeNo'; |
8 | - | { |
8 | + | |
9 | - | //Получаем json объект данных об IP пользователя |
9 | + | public function getLocation($user_ip) |
10 | - | $geo = file_get_contents("https://125111:LhIfO1JHWeNoself@geoip.maxmind.com/geoip/v2.1/insights/$user_ip"); |
10 | + | { |
11 | $authenticationKey = sprintf('%s:%s', self::USERNAME, self::PASSWORD); | |
12 | - | $user_location = []; |
12 | + | $authenticationKey = base64_encode($authenticationKey); |
13 | $httpOptions = [ | |
14 | - | /* |
14 | + | 'http' => [ |
15 | - | //одним массивом все отправлять |
15 | + | 'method' => 'GET', |
16 | 'header' => 'Authorization: Basic ' . $authenticationKey . "\r\n" | |
17 | - | $user_location = json_decode($geo, true); |
17 | + | ] |
18 | - | foreach ($data as $type => &$values) { |
18 | + | ]; |
19 | - | foreach ($values as $key => &$subValues) { |
19 | + | |
20 | - | if ($key !== 'names') { |
20 | + | $context = stream_context_create($httpOptions); |
21 | - | continue; |
21 | + | //Получаем json объект данных об IP пользователя |
22 | $geo = file_get_contents("https://geoip.maxmind.com/geoip/v2.1/insights/$user_ip", false, $context); | |
23 | - | $subValues = ['en' => $subValues['en']]; |
23 | + | $geo = json_decode($geo, true); |
24 | $user_location = []; | |
25 | - | */ |
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 | - | $user_location['location_conf'] = $geo["subdivisions"]["confidence"]; |
53 | + | |
54 | - | $user_location['location_iso_code'] = $geo["subdivisions"]["iso_code"]; |
54 | + | |
55 | - | $user_location['location_id'] = $geo["subdivisions"]["geoname_id"]; |
55 | + | |
56 | - | $user_location['location_name'] = $geo["subdivisions"]["name"]["en"]; |
56 | + | |
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 | - | return $user_location; |
64 | + | $user_location['location_conf'] = $geo["subdivisions"][0]["confidence"]; |
65 | - | } |
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); |