I’m encountering an issue with my PHP script (obtener_rutas.php
) that’s supposed to return JSON data for a routes application. The script works perfectly on my local server (XAMPP), but when deployed to my cloud server, it returns an empty JSON response.
Local server (works correctly):
- The script returns the expected JSON data with route information.
Cloud server (problem):
- The PHP script returns an empty JSON response:
[]
Here’s my obtener_rutas.php
script:
<?php
ob_start();
function debug_log($message) {
file_put_contents('debug.log', date('[Y-m-d H:i:s] ') . $message . "n", FILE_APPEND);
}
debug_log("Script started");
debug_log("SERVER: " . print_r($_SERVER, true));
error_reporting(E_ALL);
ini_set('display_errors', 0);
header('Content-Type: application/json');
function handleError($errno, $errstr, $errfile, $errline) {
$error = array(
'error' => 'PHP Error',
'message' => $errstr,
'file' => $errfile,
'line' => $errline
);
debug_log("Error occurred: " . json_encode($error));
echo json_encode($error);
exit;
}
set_error_handler('handleError');
try {
debug_log("About to require connection files");
require_once '../../inicio_sesion/conexion.php';
require_once '../../inicio_sesion/config.php';
debug_log("Connection files included");
if (!$conn) {
throw new Exception('Database connection error');
}
debug_log("Database connection successful");
$id = isset($_GET['id']) ? filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT) : null;
function getImageUrl($baseUrl, $path) {
return $baseUrl . '/paginasespecializadas/skitouring/' . $path;
}
debug_log("About to execute query");
if ($id) {
$query = "SELECT r.*,
GROUP_CONCAT(DISTINCT ri.imagen_path) as imagenes,
rp.view_count, rp.download_count, rp.favorite_count, rp.complete_count, rp.popularity_score
FROM rutas r
LEFT JOIN ruta_imagenes ri ON r.id = ri.ruta_id
LEFT JOIN route_popularity rp ON r.id = rp.route_id
WHERE r.id = ?
GROUP BY r.id";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
throw new Exception('Route not found');
}
$ruta = $result->fetch_assoc();
$ruta['imagenes'] = $ruta['imagenes'] ? explode(',', $ruta['imagenes']) : [];
// Update image paths with BASE_URL and correct folder structure
$ruta['imagenes'] = array_map(function($path) {
return getImageUrl(BASE_URL, $path);
}, $ruta['imagenes']);
debug_log("Single route processed");
echo json_encode($ruta);
} else {
$query = "SELECT r.*,
GROUP_CONCAT(DISTINCT ri.imagen_path) as imagenes,
rp.view_count, rp.download_count, rp.favorite_count, rp.complete_count, rp.popularity_score
FROM rutas r
LEFT JOIN ruta_imagenes ri ON r.id = ri.ruta_id
LEFT JOIN route_popularity rp ON r.id = rp.route_id
GROUP BY r.id
ORDER BY rp.popularity_score DESC"; // Order by popularity
debug_log("Executing all routes query");
$result = $conn->query($query);
debug_log("All routes query executed");
if (!$result) {
throw new Exception('Query error: ' . $conn->error);
}
$rutas = [];
while ($row = $result->fetch_assoc()) {
$row['imagenes'] = $row['imagenes'] ? explode(',', $row['imagenes']) : [];
$row['imagenes'] = array_map(function($path) {
return getImageUrl(BASE_URL, $path);
}, $row['imagenes']);
$rutas[] = $row;
}
debug_log("Number of routes fetched: " . count($rutas));
echo json_encode($rutas);
}
debug_log("Query results processed and sent");
} catch (Exception $e) {
http_response_code(500);
$error = ['error' => $e->getMessage()];
debug_log("Exception caught: " . json_encode($error));
echo json_encode($error);
} finally {
if (isset($stmt)) {
$stmt->close();
}
if (isset($conn) && $conn instanceof mysqli) {
$conn->close();
}
debug_log("Script ended");
}
?>
I’ve implemented logging to a debug.log file, but I’m not seeing any error messages or exceptions in the log on the cloud server.
What I’ve tried:
Checking the debug.log file for any error messages.
Verifying that the database connection details are correct for the cloud environment.
Ensuring that the file paths for conexion.php and config.php are correct on the cloud server.