diff --git a/eltiempo/js/estadisticas.js b/eltiempo/js/estadisticas.js index 227c962..2b061d0 100644 --- a/eltiempo/js/estadisticas.js +++ b/eltiempo/js/estadisticas.js @@ -1,5 +1,5 @@ // API URL absoluta -const API_URL = "https://aplicacionesdevanguardia.es/eltiempo/servidor/api-weather-reverse.php?ciudad=madrid"; +const API_URL = "https://aplicacionesdevanguardia.es/eltiempo/servidor/api-weather.php?ciudad=madrid"; // ==================== // Helper diff --git a/eltiempo/servidor/api-weather.php b/eltiempo/servidor/api-weather.php new file mode 100755 index 0000000..3c4d150 --- /dev/null +++ b/eltiempo/servidor/api-weather.php @@ -0,0 +1,87 @@ + "Parámetro 'ciudad' es requerido."]); + exit(); // Stop script execution +} + +// Optional: Further sanitize/validate the city name if needed (e.g., alphanumeric only) +// if (!preg_match('/^[a-zA-Z\s]+$/', $ciudad)) { +// http_response_code(400); +// echo json_encode(["error" => "El nombre de la ciudad contiene caracteres inválidos."]); +// exit(); +// } + +// --- 3. Database Connection --- +$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); + +if ($conn->connect_error) { + http_response_code(500); // Internal Server Error + echo json_encode(["error" => "Error de conexión a la base de datos: " . $conn->connect_error]); + exit(); +} + +// --- 4. Prepare and Execute Query --- +$stmt = $conn->prepare(" + SELECT DATE(fecha) AS dia, + MIN(fecha) AS primera_fecha_del_dia, + MIN(amanecer) AS amanecer, + MAX(anochecer) AS anochecer, + MAX(temp_max) AS temp_max, + MIN(temp_min) AS temp_min, + AVG(humedad) AS humedad, + AVG(lluvia) AS lluvia, + AVG(nubes) AS nubes, + AVG(viento_velocidad) AS viento_velocidad, + AVG(viento_direccion) AS viento_direccion + FROM weather + WHERE DATE(fecha) >= '2024-10-01' + AND ciudad LIKE CONCAT('%', ?, '%') + GROUP BY DATE(fecha) + ORDER BY DATE(fecha) +"); + +if (!$stmt) { + http_response_code(500); // Internal Server Error + echo json_encode(["error" => "Error al preparar la consulta: " . $conn->error]); + $conn->close(); + exit(); +} + +$stmt->bind_param("s", $ciudad); +$stmt->execute(); +$result = $stmt->get_result(); + +$datos = []; +if ($result->num_rows > 0) { + while ($row = $result->fetch_assoc()) { + $datos[] = $row; + } + http_response_code(200); // OK + echo json_encode($datos); +} else { + http_response_code(200); + echo json_encode([]); // array vacío +} + + +// --- 5. Close Resources --- +$stmt->close(); +$conn->close(); +?> diff --git a/eltiempo/servidor/weather-hoy.php b/eltiempo/servidor/weather-hoy.php new file mode 100644 index 0000000..a12e0eb --- /dev/null +++ b/eltiempo/servidor/weather-hoy.php @@ -0,0 +1,41 @@ +connect_error) { + die("Connection failed: " . $conn->connect_error); +} + +$ciudad = $_GET['ciudad'] ?? ''; +$hoy = date("Y-m-d"); + +$sql = "SELECT amanecer, anochecer, MIN(temp_min), MAX(temp_max), MAX(viento_velocidad), AVG(viento_direccion), MAX(nubes), MAX(lluvia) + FROM weather + WHERE fecha LIKE '%" . $hoy . "%' AND ciudad LIKE '%" . $ciudad . "%'"; + +$result = $conn->query($sql); + +$data = array(); +if ($result->num_rows > 0) { + while($row = $result->fetch_assoc()) { + $data[] = $row; + } + echo json_encode($data); +} else { + echo json_encode(array("message" => "No data found")); +} +$conn->close(); +?>