承接 dominik-eller/geoip 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

dominik-eller/geoip

Composer 安装命令:

composer require dominik-eller/geoip

包简介

Google Ads GeoTargets CSV updater and lookup as a reusable PHP 8.4 Composer package.

README 文档

README

Small PHP 8.4 library and CLI to download the official Google Ads GeoTargets list and look up entries by criteria_id (aka loc_physical_ms).

Installation

composer require dominik-eller/geoip

Requirements:

  • PHP 8.4+
  • ext-zip and ext-json

Quick start (library)

use Deller\GeoIP\GeoTargetsUpdater;
use Deller\GeoIP\GeoTargetsLookup;

// 1) Download/refresh CSV (once or via cron)
$targetDir = __DIR__ . '/var/geodata';
$updater = new GeoTargetsUpdater($targetDir);
$csvPath = $updater->update(); // returns /absolute/path/to/geotargets.csv

// 2) Lookup by criteria_id (aka loc_physical_ms)
$lookup = new GeoTargetsLookup($csvPath);
$result = $lookup->findById(2267);

header('Content-Type: application/json; charset=utf-8');
echo json_encode($result ?: ['error' => 'not_found'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

Returned structure on success:

{
  "criteria_id": "2267",
  "name": "Germany",
  "canonical_name": "Germany",
  "country_code": "DE",
  "target_type": "Country",
  "status": "Active"
}

If not found:

{"error": "not_found", "criteria_id": "<id>"}

CLI tools

Two executables are provided via Composer bin:

  • geotargets-update — downloads the latest CSV
  • geotargets-lookup — looks up an ID and prints JSON

Examples:

# Download to ./data by default
vendor/bin/geotargets-update

# Set custom directory
GEOTARGETS_DIR=/var/data/geo vendor/bin/geotargets-update

# Lookup by ID
vendor/bin/geotargets-lookup 2267

# Or via env var
LOC=2267 vendor/bin/geotargets-lookup

# Custom CSV path
GEOTARGETS_CSV=/var/data/geo/geotargets.csv vendor/bin/geotargets-lookup 2267

WordPress Plugin

Dieses Paket enthält ein WordPress-Plugin für eine einfache Integration.

Build (ZIP erstellen)

Da das Plugin die Kern-Bibliothek nutzt, aber ohne Composer lauffähig sein soll, muss es "gebaut" werden. Dabei werden die notwendigen Dateien in ein installierbares ZIP-Archiv gepackt.

composer build-wp-plugin

Das Ergebnis liegt unter build/deller-geoip-plugin.zip.

Installation

  1. Erstelle das ZIP-Archiv wie oben beschrieben.
  2. Lade das ZIP-Archiv im WordPress-Adminbereich unter Plugins -> Installieren -> Plugin hochladen hoch.
  3. Alternativ: Kopiere den Inhalt des wp-plugin Verzeichnisses manuell in wp-content/plugins/geoip und stelle sicher, dass die Dateien aus src/ ebenfalls im src/ Ordner des Plugins liegen.
  4. Aktiviere das Plugin im WordPress-Adminbereich.

Features

  • WP-CLI Command: wp geoip update to refresh the GeoTargets data.
    • Optional: wp geoip update --country=DE to only download targets for a specific country (reduces file size significantly).
  • WP-CLI Command: wp geoip lookup <id> to check a specific ID.
  • Helper Function: Use deller_geoip_lookup($id) in your own plugins or themes.
  • Data Storage: The CSV data is stored in your WordPress uploads directory under wp-content/uploads/geoip-data/.
  • Automatic Updates: The plugin schedules a monthly WP-Cron event (deller_geoip_update_event) to keep the data fresh.
    • Tip: You can hook into deller_geoip_update_event and pass a country code if you want the automated update to be filtered.

Manual Cron Trigger

If you want to trigger the update via a real system cron job, you have several options:

1. Via Standalone PHP script (Recommended for Plesk/Standard Hosting)

The plugin provides a dedicated script that can be called directly:

# Update all targets
php wp-content/plugins/geoip/cron-update.php

# Update only Germany (DE)
php wp-content/plugins/geoip/cron-update.php DE

This is the easiest way if you don't have WP-CLI installed.

2. Via WP-CLI

wp geoip update

3. Via Action Hook

Or hook into the action within your own code:

do_action('deller_geoip_update_event');

Example usage in code

$data = deller_geoip_lookup(1004542);
if ($data) {
    echo "Location: " . $data['canonical_name'];
}

Web endpoint example

Create public/geotarget.php in your app:

<?php
require __DIR__ . '/../vendor/autoload.php';

use Deller\GeoIP\GeoTargetsLookup;

$csv = __DIR__ . '/../var/geodata/geotargets.csv';
$loc = $_GET['loc'] ?? $_GET['loc_physical_ms'] ?? null;

header('Content-Type: application/json; charset=utf-8');
if (!$loc) {
    http_response_code(400);
    echo json_encode(['error' => 'missing_parameter', 'param' => 'loc_physical_ms']);
    exit;
}

try {
    $lookup = new GeoTargetsLookup($csv);
    $row = $lookup->findById($loc);
    echo json_encode($row ?: ['error' => 'not_found', 'criteria_id' => (string)$loc], JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['error' => 'lookup_failed', 'message' => $e->getMessage()]);
}

Cron suggestion

Update monthly (Google updates roughly monthly):

0 3 1 * * /usr/bin/env php /path/to/your/app/vendor/bin/geotargets-update >> /var/log/geotargets_update.log 2>&1

JSON response spec

  • Success object:
    • criteria_id string
    • name string
    • canonical_name string
    • country_code string (2-letter)
    • target_type string (e.g., Country, City, …)
    • status string (Active/Deprecated)
  • Error object:
    • error one of missing_parameter, not_found, lookup_failed
    • optional criteria_id and message

Notes

  • The downloader scrapes the official docs page to find the current geotargets.zip/geotargets.csv link. If Google changes the page structure, override the source with GEOTARGETS_SOURCE or pass the URL to GeoTargetsUpdater::update($url).
  • The library writes an optional last_update.txt file next to the CSV.

Tests & Examples

  • Tests use PHPUnit and run offline with fixtures.
  • Run tests:
    composer install
    composer test
    
  • Example scripts are in examples/:
    • examples/update.php
    • examples/lookup.php
    • examples/web-endpoint.php (serve with php -S 127.0.0.1:8080 -t examples)

License

MIT License. See LICENSE.

dominik-eller/geoip 适用场景与选型建议

dominik-eller/geoip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 dominik-eller/geoip 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-12