setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $log = "=== Inicio Sincronización: " . date('Y-m-d H:i:s') . " ===\n"; // 2. Recorrer tablas y enviar foreach ($tablas_a_sincronizar as $tabla) { // ✅ CORRECCIÓN DEFINITIVA: Solo las 6 columnas que existen en AMBOS servidores if ($tabla === 'cabinas') { $stmt = $pdo_local->query("SELECT id, codigo, fila, nivel, estado, ubicacion FROM cabinas"); } else { $stmt = $pdo_local->query("SELECT * FROM $tabla"); } $datos = $stmt->fetchAll(PDO::FETCH_ASSOC); if (empty($datos)) { $log .= "Tabla '$tabla': Sin datos. Omitida.\n"; continue; } // 3. Enviar a RUNHOSTING vía cURL $payload = json_encode([ 'secret_key' => $secret_key, 'tabla' => $tabla, 'datos' => $datos ]); $ch = curl_init($remote_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $respuesta = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $resultado = json_decode($respuesta, true); if ($http_code == 200 && isset($resultado['success'])) { $log .= "Tabla '$tabla': OK. " . ($resultado['actualizados'] ?? count($datos)) . " registros sincronizados.\n"; } else { $log .= "Tabla '$tabla': ERROR. Código $http_code. Respuesta: $respuesta\n"; } } // 4. Guardar log file_put_contents('/var/www/html/api/sync_log.txt', $log, FILE_APPEND); echo $log; } catch (Exception $e) { echo "Error fatal: " . $e->getMessage() . "\n"; } ?>