maxmind-db/reader 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maxmind-db/reader

Composer 安装命令:

composer require maxmind-db/reader

包简介

MaxMind DB Reader API

README 文档

README

Description

This is the PHP API for reading MaxMind DB files. MaxMind DB is a binary file format that stores data indexed by IP address subnets (IPv4 or IPv6).

Installation

C Extension (Recommended for Performance)

For significantly faster IP lookups, we recommend installing the C extension via PIE:

pie install maxmind-db/reader-ext

The C extension requires the libmaxminddb C library. See the installation instructions for your platform.

Pure PHP (No Compilation Required)

If you prefer not to compile a C extension or need maximum portability, you can install the pure PHP implementation with Composer.

Download Composer

To download Composer, run in the root directory of your project:

curl -sS https://getcomposer.org/installer | php

You should now have the file composer.phar in your project directory.

Install Dependencies

Run in your project root:

php composer.phar require maxmind-db/reader:^1.13.1

You should now have the files composer.json and composer.lock as well as the directory vendor in your project directory. If you use a version control system, composer.json should be added to it.

Require Autoloader

After installing the dependencies, you need to require the Composer autoloader from your code:

require 'vendor/autoload.php';

Installation (Standalone)

If you don't want to use Composer for some reason, a custom autoload.php is provided for you in the project root. To use the library, simply include that file,

require('/path/to/MaxMind-DB-Reader-php/autoload.php');

and then instantiate the reader class normally:

use MaxMind\Db\Reader;
$reader = new Reader('example.mmdb');

Installation (RPM)

RPMs are available in the official Fedora repository.

To install on Fedora, run:

dnf install php-maxminddb

To install on CentOS or RHEL 7, first enable the EPEL repository and then run:

yum install php-maxminddb

Please note that these packages are not maintained by MaxMind.

Usage

Example

<?php
require_once 'vendor/autoload.php';

use MaxMind\Db\Reader;

$ipAddress = '24.24.24.24';
$databaseFile = 'GeoIP2-City.mmdb';

$reader = new Reader($databaseFile);

// get returns just the record for the IP address
print_r($reader->get($ipAddress));

// getWithPrefixLen returns an array containing the record and the
// associated prefix length for that record.
print_r($reader->getWithPrefixLen($ipAddress));

$reader->close();

Optional PHP C Extension

MaxMind provides an optional C extension that is a drop-in replacement for MaxMind\Db\Reader. In order to use this extension, you must install the Reader API as described above and install the extension as described below. If you are using an autoloader, no changes to your code should be necessary.

Installing Extension via PIE (Recommended)

We recommend installing the extension via PIE:

pie install maxmind-db/reader-ext

See the extension repository for prerequisites including libmaxminddb installation instructions.

Installing Extension via PECL (Legacy)

First install libmaxminddb as described in its README.md file. After successfully installing libmaxmindb, you may install the extension from PECL:

pecl install maxminddb

Installing Extension from Source

Alternatively, you may install it from the source. To do so, run the following commands from the top-level directory of this distribution:

cd ext
phpize
./configure
make
make test
sudo make install

You then must load your extension. The recommended method is to add the following to your php.ini file:

extension=maxminddb.so

Note: You may need to install the PHP development package on your OS such as php5-dev for Debian-based systems or php-devel for RedHat/Fedora-based ones.

128-bit Integer Support

The MaxMind DB format includes 128-bit unsigned integer as a type. Although no MaxMind-distributed database currently makes use of this type, both the pure PHP reader and the C extension support this type. The pure PHP reader requires gmp or bcmath to read databases with 128-bit unsigned integers.

The integer is currently returned as a hexadecimal string (prefixed with "0x") by the C extension and a decimal string (no prefix) by the pure PHP reader. Any change to make the reader implementations always return either a hexadecimal or decimal representation of the integer will NOT be considered a breaking change.

Support

Please report all issues with this code using the GitHub issue tracker.

If you are having an issue with a MaxMind service that is not specific to the client API, please see our support page.

Requirements

This library requires PHP 7.2 or greater.

The GMP or BCMath extension may be required to read some databases using the pure PHP API.

Contributing

Patches and pull requests are encouraged. All code should follow the PSR-1 and PSR-2 style guidelines. Please include unit tests whenever possible.

Versioning

The MaxMind DB Reader PHP API uses Semantic Versioning.

Copyright and License

This software is Copyright (c) 2014-2025 by MaxMind, Inc.

This is free software, licensed under the Apache License, Version 2.0.

maxmind-db/reader 适用场景与选型建议

maxmind-db/reader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 105.83M 次下载、GitHub Stars 达 698, 最近一次更新时间为 2013 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「geolocation」 「maxmind」 「geoip」 「geoip2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 maxmind-db/reader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 maxmind-db/reader 我们能提供哪些服务?
定制开发 / 二次开发

基于 maxmind-db/reader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 105.83M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 709
  • 点击次数: 24
  • 依赖项目数: 60
  • 推荐数: 2

GitHub 信息

  • Stars: 698
  • Watchers: 37
  • Forks: 82
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2013-07-12