<?php
// -----------------------------
// CONFIG
// -----------------------------
$urls = [
    "results" => "https://satta4umatka.live/tools/auto_result/",
    "freegame" => "https://satta4umatka.live/tools/free_auto/",
    //"home" => "https://indiamatka.org/tools/home"
];

$cacheDir = __DIR__ . "/cache";
$cacheTime = 60; // 60 seconds

// -----------------------------
// CREATE CACHE FOLDER IF NOT EXISTS
// -----------------------------
if(!file_exists($cacheDir)){
    mkdir($cacheDir, 0755, true);
}

// -----------------------------
// LOOP THROUGH ALL URLS
// -----------------------------
foreach($urls as $key => $url){
    $cacheFile = "$cacheDir/{$key}_cache.json";

    if(file_exists($cacheFile) && (time() - filemtime($cacheFile) < $cacheTime)){
        echo "Using cached data for $key. Last updated: ".date("H:i:s", filemtime($cacheFile))."\n";
        $data = file_get_contents($cacheFile);
    } else {
        // FETCH DATA VIA cURL
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $data = curl_exec($ch);
        curl_close($ch);

        // SAVE TO CACHE
        file_put_contents($cacheFile, $data);
        echo "Fetched fresh data for $key at ".date("H:i:s")."\n";
    }

    // OPTIONAL: decode JSON
    $result[$key] = json_decode($data, true);
}

// DEBUG: print all results
print_r($result);
