geocoder-php/maxmind-binary-provider
MaxMind Binary provider for the PHP Geocoder library. Lookup geolocation data from local MaxMind GeoIP2/GeoLite2 binary databases without external API calls. Useful for fast, offline IP-to-location resolution in PHP apps.
Installation
composer require geocoder-php/maxmind-binary-provider
Ensure geocoder-php/geocoder is also installed (required dependency).
Basic Setup
GeoLite2-City.mmdb)..mmdb file in a secure, accessible directory (e.g., storage/maxmind/).First Query
use GeoCoder\Geocoder;
use GeoCoder\Provider\MaxMind\BinaryProvider;
$geocoder = new Geocoder();
$geocoder->addProvider(BinaryProvider::fromPath('/path/to/GeoLite2-City.mmdb'));
$result = $geocoder->geocode('192.0.2.1'); // IP address
// or
$result = $geocoder->reverse('192.0.2.1'); // Reverse geocoding (if supported)
Key Files to Reference
vendor/geocoder-php/maxmind-binary-provider/src/Provider/BinaryProvider.php (core logic).IP Geocoding
$geocoder->geocode('8.8.8.8')->get();
// Returns collection of locations (e.g., city, country, coordinates).
Reverse Geocoding (Limited)
GoogleMapsProvider).Caching Responses
$geocoder->addProvider(
BinaryProvider::fromPath('/path/to/db.mmdb')
->withCache(new \GeoCoder\Cache\ArrayCache())
);
Environment-Specific Config
// config/geocoder.php
'providers' => [
'maxmind' => [
'path' => env('MAXMIND_DB_PATH', storage_path('maxmind/GeoLite2-City.mmdb')),
'cache' => env('GEOCODER_CACHE', false),
],
];
Integration with Laravel Requests
use Illuminate\Http\Request;
public function getUserLocation(Request $request)
{
$ip = $request->ip();
$location = app(Geocoder::class)->geocode($ip)->first();
return $location->getCoordinates();
}
Fallback Providers
$geocoder->addProvider(BinaryProvider::fromPath('/path/to/db.mmdb'));
$geocoder->addProvider(new \GeoCoder\Provider\FreeGeoIpProvider()); // Fallback
Batch Processing
$ips = ['192.0.2.1', '198.51.100.1'];
$results = $geocoder->geocodeBatch($ips);
Custom Data Extraction
$location = $geocoder->geocode('1.1.1.1')->first();
$country = $location->getCountry()->getName();
$latitude = $location->getCoordinates()->getLatitude();
Database File Permissions
.mmdb file is readable by the web server (e.g., chmod 644 storage/maxmind/*.mmdb).MaxMind\Exception\InvalidDatabaseException or empty results.Outdated Databases
spatie/laravel-maxmind).IPv6 Support
::1 (IPv6 localhost).Memory Usage
$provider->withCache(new \GeoCoder\Cache\FileCache(storage_path('cache/geocoder')));
Time Zone Handling
$timeZone = $location->getTimeZone()->getCode(); // e.g., "America/New_York"
Verify Database Validity
use MaxMind\Db\Reader\InvalidDatabaseException;
try {
$reader = new \MaxMind\Db\Reader('/path/to/db.mmdb');
} catch (InvalidDatabaseException $e) {
// Log or alert: Database is corrupted or invalid.
}
Log Queries
$geocoder->addProvider(
BinaryProvider::fromPath('/path/to/db.mmdb')
->withLogger(new \Monolog\Logger('geocoder'))
);
Check for Empty Results
$result = $geocoder->geocode('invalid.ip.address');
if ($result->isEmpty()) {
// Handle missing data (e.g., fallback to user input or default location).
}
Custom Data Mappers
Override default field mappings (e.g., rename city.names.en to city_name):
$provider = BinaryProvider::fromPath('/path/to/db.mmdb');
$provider->setDataMapper(new \GeoCoder\Provider\MaxMind\CustomDataMapper());
Event Listeners Extend the provider to trigger events (e.g., on cache miss):
$provider->onCacheMiss(function ($ip) {
// Log or trigger analytics.
});
Laravel Service Provider Bind the geocoder to the container for dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Geocoder::class, function () {
$geocoder = new Geocoder();
$geocoder->addProvider(BinaryProvider::fromPath(config('geocoder.maxmind.path')));
return $geocoder;
});
}
Testing Mock the provider for unit tests:
$mockProvider = $this->createMock(BinaryProvider::class);
$mockProvider->method('geocode')->willReturn([new \GeoCoder\Model\Address()]);
$geocoder->addProvider($mockProvider);
How can I help you explore Laravel packages today?