PHP script returns empty JSON on cloud server but works locally – How to debug?

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.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật