maxmind-db/reader
PHP library for reading MaxMind DB (.mmdb) files to look up data by IPv4/IPv6 subnet. Supports fast lookups via optional C extension (libmaxminddb) or a pure PHP implementation installed with Composer. Suitable for GeoIP-style IP metadata queries.
Start by installing the package via Composer: composer require maxmind-db/reader:^1.13. Then include the autoloader with require 'vendor/autoload.php';. Your first use case is likely IP geolocation—download a MaxMind DB file (e.g., GeoLite2-City.mmdb from MaxMind’s free GeoLite2), and write a quick lookup:
use MaxMind\Db\Reader;
$reader = new Reader('/path/to/GeoIP2-City.mmdb');
$record = $reader->get('8.8.8.8');
print_r($record); // Returns city/country data
$reader->close();
If performance is critical, install the C extension via PIE: pie install maxmind-db/reader-ext—this is a drop-in performance boost with zero code changes.
Reader once per request (e.g., via DI container or static lazy loader), since opening large .mmdb files repeatedly is expensive.try/catch for MaxMind\Db\Reader\InvalidDatabaseException and handle missing or malformed DB files gracefully (e.g., fallback to a default locale).getWithPrefixLen() when you need to know the exact subnet match (e.g., for fraud scoring logic).GeoIpService bound as a singleton, inject it into controllers, and register a ServiceProvider that lazily initializes the reader on first access.Reader class (it has clear interfaces) and test against a fixture .mmdb file (e.g., GeoLite2-City.mmdb from your test fixtures directory).uint64 field values) may be returned as strings on 32-bit platforms unless gmp or bcmath is installed. On 32-bit systems without these extensions, 1.10.0+ now returns integers up to PHP_INT_MAX—verify behavior with your DB schema.open_basedir Restriction: The C extension enforces open_basedir, but the pure PHP reader does not. This can cause inconsistent behavior in shared hosting environments—always test deployment parity.0x...). Avoid mixing DBs or versions in multi-server setups.$reader->close() (or rely on __destruct() in CLI scripts), but be explicit in FPM to prevent resource leaks in long-lived processes (e.g., Swoole, ReactPHP).$reader->metadata() to validate DB format and checksums during startup, especially in CI or deployment pipelines.How can I help you explore Laravel packages today?