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).
- Source page: https://developers.google.com/google-ads/api/data/geotargets
- Stores a local
geotargets.csv - Lookup by
criteria_idand return a JSON-compatible array - Includes CLI tools for update and lookup
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 CSVgeotargets-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
- Erstelle das ZIP-Archiv wie oben beschrieben.
- Lade das ZIP-Archiv im WordPress-Adminbereich unter Plugins -> Installieren -> Plugin hochladen hoch.
- Alternativ: Kopiere den Inhalt des
wp-pluginVerzeichnisses manuell inwp-content/plugins/geoipund stelle sicher, dass die Dateien aussrc/ebenfalls imsrc/Ordner des Plugins liegen. - Aktiviere das Plugin im WordPress-Adminbereich.
Features
- WP-CLI Command:
wp geoip updateto refresh the GeoTargets data.- Optional:
wp geoip update --country=DEto only download targets for a specific country (reduces file size significantly).
- Optional:
- 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_eventand pass a country code if you want the automated update to be filtered.
- Tip: You can hook into
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_idstringnamestringcanonical_namestringcountry_codestring (2-letter)target_typestring (e.g., Country, City, …)statusstring (Active/Deprecated)
- Error object:
errorone ofmissing_parameter,not_found,lookup_failed- optional
criteria_idandmessage
Notes
- The downloader scrapes the official docs page to find the current
geotargets.zip/geotargets.csvlink. If Google changes the page structure, override the source withGEOTARGETS_SOURCEor pass the URL toGeoTargetsUpdater::update($url). - The library writes an optional
last_update.txtfile 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.phpexamples/lookup.phpexamples/web-endpoint.php(serve withphp -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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dominik-eller/geoip 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
GeoMate is a friend in need for all things geolocation. IP to geo lookup, automatic redirects (based on country, continent, language, etc), site switcher... You name it.
Show geo marker on a mapbox map
Show geo shape on a mapbox map with drawing capabilities
The official PHP library for apiip.net that allowing customers to automate IP address validation and geolocation lookup in websites, applications and back-office system. Visit our API docs at https://apiip.net/documentation
Yii2 lookup module
A Symfony2 Bundle to handle geographic location of your entities
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-12