Created
January 28, 2018 21:29
-
-
Save leotop/77527e37d88cb1d132122e56f860887a to your computer and use it in GitHub Desktop.
Пример работы с гео данными https://github.com/maxmind/GeoIP2-php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
use GeoIp2\Database\Reader; | |
// This creates the Reader object, which should be reused across | |
// lookups. | |
$reader = new Reader('GeoLite2-City.mmdb'); | |
// Replace "city" with the appropriate method for your database, e.g., | |
// "country". | |
function getUserIP() | |
{ | |
$client = @$_SERVER['HTTP_CLIENT_IP']; | |
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; | |
$remote = $_SERVER['REMOTE_ADDR']; | |
if(filter_var($client, FILTER_VALIDATE_IP)) | |
{ | |
$ip = $client; | |
} | |
elseif(filter_var($forward, FILTER_VALIDATE_IP)) | |
{ | |
$ip = $forward; | |
} | |
else | |
{ | |
$ip = $remote; | |
} | |
return $ip; | |
} | |
$user_ip = getUserIP(); | |
echo $user_ip; // Output IP address [Ex: 177.87.193.134] | |
$record = $reader->city($user_ip); | |
print($record->country->isoCode . "\n"); // 'US' | |
print($record->country->name . "\n"); // 'United States' | |
print($record->country->names['ru'] . "\n"); // '美国' | |
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota' | |
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN' | |
print($record->city->name . "\n"); // 'Minneapolis' | |
print($record->postal->code . "\n"); // '55455' | |
print($record->location->latitude . "\n"); // 44.9733 | |
print($record->location->longitude . "\n"); // -93.2323 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment