<?php
// Run on your server with PHP and the cURL extension. Keep credentials in the environment.
$key = getenv('LOCATIONIQ_KEY') ?: '';
$mode = getenv('LOCATIONIQ_ENV') ?: 'sandbox';
if (!in_array($mode, ['sandbox', 'production'], true)) { fwrite(STDERR, "Invalid LOCATIONIQ_ENV\n"); exit(1); }
$prefix = $mode === 'sandbox' ? 'tz_test_' : 'tz_live_';
if (strncmp($key, $prefix, strlen($prefix)) !== 0) { fwrite(STDERR, "Set LOCATIONIQ_KEY for this environment\n"); exit(1); }
$path = $mode === 'sandbox' ? '/sandbox/v1/reverse' : '/v1/reverse';
$ch = curl_init('https://locationiq.co.tz' . $path . '?latitude=-6.81&longitude=39.28');
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_TIMEOUT => 20, CURLOPT_HTTPHEADER => ['X-API-Key: ' . $key, 'Accept: application/json']]);
$body = curl_exec($ch);
if ($body === false) { fwrite(STDERR, "Connection failed\n"); curl_close($ch); exit(1); }
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($body, true);
if ($status < 200 || $status >= 300) { fwrite(STDERR, 'HTTP ' . $status . ': ' . ($data['detail'] ?? 'Request failed') . "\n"); exit(1); }
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
