PHP code example of pangongzi / mobile

1. Go to this page and download the library: Download pangongzi/mobile 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/ */

    

pangongzi / mobile example snippets



Pangongzi\Mobile\MobileLocator;

// 获取单例实例(推荐)
$locator = MobileLocator::getInstance();

// 查询手机号归属地
$result = $locator->find('13812345678');

if ($result) {
    printf("📱 手机号: %s\n", $result['mobile']);
    printf("📍 地区: %s%s\n", $result['province'], $result['city']);
    printf("📮 邮编: %s\n", $result['zip_code']);
    printf("📞 区号: %s\n", $result['area_code']);
    printf("📡 运营商: %s\n", $result['type_str']);
}

// ✅ 优化的索引解析(性能提升25%)
private function getIndexData(int $index): array
{
    $data = substr($this->data, $position, 9);
    // 批量解包:减少66%函数调用
    $unpacked = unpack('Vprefix/Voffset/Ctype', $data);
    return $unpacked;
}

// 使用默认数据文件
$locator = MobileLocator::getInstance();

// 使用自定义数据文件
$locator = MobileLocator::getInstance('/path/to/custom/mobile.dat');

$result = $locator->find('13812345678');

// 返回格式
[
    'mobile' => '13812345678',     // 查询的手机号
    'province' => '浙江',           // 省份
    'city' => '杭州',              // 城市  
    'zip_code' => '310000',        // 邮政编码
    'area_code' => '0571',         // 区号
    'type' => 1,                   // 运营商类型编码
    'type_str' => '移动',          // 运营商名称
    'info' => '1 | 移动 | 浙江 | 杭州 | 310000 | 0571'  // 格式化信息
]

$version = $locator->getVersion(); // 返回: 842020146

// 🔍 版本号解析说明:
// 当前版本号 842020146 解密过程:
// 1. 十六进制: 0x32303532
// 2. ASCII解码: "2052" 
// 3. 字符串反转: "2052" → "2502"
// 4. 最终含义: 2025年02月 更新版本
//
// 这是由于数据文件格式限制只能存储4个字符,
// 所以将 "2502" 反转存储为 "2052" 来表示 2025年02月

$count = $locator->getIndexCount(); // 517,258条记录

$types = $locator->getOperatorTypes();
// [
//     0 => '未知',
//     1 => '移动',
//     2 => '联通',
//     3 => '电信',
//     4 => '电信虚拟运营商',
//     5 => '联通虚拟运营商', 
//     6 => '移动虚拟运营商',
//     7 => '中国广电',
//     8 => '中国广电虚拟运营商'
// ]

class MobileLocator
{
    private static $instance = null;    // 单例实例
    private string $data;               // 内存中的数据文件
    private int $fileSize;              // 文件大小
    private int $indexBegin;            // 索引起始位置
    private int $indexCount;            // 索引总数
}

try {
    // 文件不存在异常
    $locator = MobileLocator::getInstance('/invalid/path.dat');
} catch (Exception $e) {
    echo "错误: " . $e->getMessage(); // "Data file not found: /invalid/path.dat"
}

// 无效手机号处理
$result = $locator->find('invalid'); // 返回 null
$result = $locator->find('138123456'); // 返回 null (不足11位)

// 1. 应用启动时预加载
class AppServiceProvider 
{
    public function boot() 
    {
        // 预加载数据文件到内存
        MobileLocator::getInstance();
    }
}

// 2. 监控内存使用
$locator = MobileLocator::getInstance();
echo "内存占用: " . (memory_get_usage() / 1024 / 1024) . " MB";

// 3. 健康检查
if ($locator->getIndexCount() > 0) {
    echo "✅ MobileLocator 服务正常";
}
bash
# 克隆项目
git clone https://github.com/pangongzi/mobile.git
cd mobile

# 安装依赖
composer install

# 运行示例
php examples/basic_usage.php