PHP code example of danielme85 / laravel-geoip2
1. Go to this page and download the library: Download danielme85/laravel-geoip2 library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
danielme85 / laravel-geoip2 example snippets
return [
'downloadUrl' => 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz',
'folder' => 'app/geoip2', // path inside storage/
'filename' => 'GeoLite2-City.mmdb',
'localhost' => '8.8.8.8', // fallback IP when running locally
'license' => env('GEOIP2_LICENSE', ''),
];
use danielme85\Geoip2\Facade\Reader;
$reader = Reader::connect();
$city = $reader->city('8.8.8.8');
echo $city->city->name; // "Mountain View"
echo $city->country->isoCode; // "US"
echo $city->location->latitude; // 37.386
echo $city->location->longitude; // -122.0838
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $request->ip();
try {
$geodata = Reader::connect()->city($ip)->jsonSerialize();
} catch (\GeoIp2\Exception\AddressNotFoundException $e) {
// IP not found in database
}
bash
php artisan vendor:publish --provider="danielme85\Geoip2\Geoip2ServiceProvider"
bash
php artisan geoip:download