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.
maxmind-binary-provider package is a read-only provider for the Geocoder PHP library, enabling integration with MaxMind’s GeoIP2 binary databases (e.g., GeoLite2-City.mmdb). It fits well in architectures requiring IP-to-geolocation resolution, such as:
GoogleMapsProvider, NominatimProvider) without refactoring core logic. This aligns with SOLID principles (Dependency Inversion).geocoder-php/geocoder (v4+) and geocoder-php/maxmind-binary-provider.GeoLite2-City.mmdb) in a readable path.use Geocoder\Geocoder;
use Geocoder\Provider\MaxMind\BinaryProvider;
$geocoder = new Geocoder();
$geocoder->registerProvider('maxmind', new BinaryProvider('/path/to/GeoLite2-City.mmdb'));
$result = $geocoder->geocode('8.8.8.8')->first();
geocode() method..mmdb file (can use MaxMind’s free GeoLite2 test data).| Risk Area | Mitigation Strategy |
|---|---|
| License Compliance | Verify MaxMind’s EULA for production use (free tier has limitations). |
| Database Updates | Automate updates (e.g., cron job to fetch new .mmdb files from MaxMind’s CDN). |
| Accuracy | Free tiers (e.g., GeoLite2) have lower precision than paid GeoIP2 databases. |
| File Size | GeoLite2-City.mmdb (~6MB); GeoIP2-City (~10MB). May bloat deployments if not cached. |
| Deprecation | Monitor Geocoder’s roadmap for API changes. |
GoogleMapsProvider, IP2Location)?.mmdb files (e.g., AWS Lambda layers).composer require geocoder-php/geocoder geocoder-php/maxmind-binary-provider
.mmdb file path in config/services.php (Laravel example):
'geocoder' => [
'maxmind_path' => storage_path('app/GeoLite2-City.mmdb'),
],
$geocoder = new \Geocoder\Geocoder();
$geocoder->registerProvider('maxmind', new \Geocoder\Provider\MaxMind\BinaryProvider(config('geocoder.maxmind_path')));
app()->singleton('geocoder', fn() => $geocoder);
public function handle(Request $request, Closure $next) {
$ip = $request->ip();
$location = app('geocoder')->geocode($ip)->first();
$request->merge(['user_location' => $location]);
return $next($request);
}
$geocoder = app('geocoder');
$users = User::all();
foreach ($users as $user) {
$user->location = $geocoder->geocode($user->ip)->first();
$user->save();
}
.mmdb files. Other formats (e.g., CSV) require different providers..mmdb path for different environments (dev/staging/prod)..mmdb files (e.g., using curl or Guzzle) and replace the old file (atomic swap recommended)..mmdb files, missing IPs).try {
$location = $geocoder->geocode($ip)->first();
} catch (\Exception $e) {
Log::error("Geocode failed for IP {$ip}: " . $e->getMessage());
}
How can I help you explore Laravel packages today?