<?php
header("Content-Type: application/json");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// API URL
$url = "https://ipapi.co/json/";

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // In case SSL verification causes issues
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Set a timeout to avoid hanging requests

// Execute cURL request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Check if request was successful
if ($http_code == 200 && $response) {
    echo $response;
} else {
    echo json_encode(["error" => "Unable to fetch location"]);
}
?>
