<?php
// test_dof.php
// Script independiente para probar la conectividad desde el servidor hacia las APIs del DOF

$urls_a_probar = [
    'DOF Producción' => 'https://sidof.segob.gob.mx/dof/sidof/indicadores/',
    'DOF QA'         => 'https://sidofqa.segob.gob.mx/dof/sidof/indicadores/'
];

echo "<div style='font-family: monospace; padding: 20px;'>";
echo "<h2>Prueba de Conectividad a Servicios del DOF</h2>";
echo "<b>IP Pública (salida) de este servidor:</b> " . file_get_contents('https://api.ipify.org') . "<br><br>";

foreach ($urls_a_probar as $nombre => $url) {
    echo "<h3>Probando: {$nombre}</h3>";
    echo "URL: {$url}<br>";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true); // Solo headers para ver si conecta
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    // Fingir ser un navegador (User-Agent real) y forzar IPv4 por si acaso
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36');
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

    $inicio = microtime(true);
    $response = curl_exec($ch);
    $tiempo = round(microtime(true) - $inicio, 2);
    
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if (curl_errno($ch)) {
        echo "<span style='color: red;'><b>Error cURL:</b> " . curl_error($ch) . "</span><br>";
    } else {
        if ($httpCode == 200) {
            echo "<span style='color: green;'><b>Conexión Exitosa:</b> HTTP 200 OK</span><br>";
        } else {
            echo "<span style='color: orange;'><b>Respuesta Inesperada:</b> HTTP {$httpCode}</span><br>";
        }
    }
    echo "<b>Tiempo de respuesta:</b> {$tiempo} segundos<br>";
    curl_close($ch);
    echo "<hr>";
}

echo "</div>";
