PHP脚本云服务器返回空JSON但本地正常,求调试方法
PHP脚本部署云服务器后返回空JSON问题
我有一个PHP脚本obtener_rutas.php,用于为路线应用返回JSON数据。本地XAMPP环境下运行正常,能返回预期的路线信息JSON,但部署到云服务器后,返回空JSON响应:[]。
我已实现debug.log日志记录,但云服务器上的日志文件没有任何错误消息或异常记录。
脚本代码
<?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"); } ?>
已尝试的排查操作
- 检查
debug.log文件是否有错误消息; - 验证云环境下的数据库连接信息是否正确;
- 确保云服务器上
conexion.php和config.php的文件路径正确。
内容的提问来源于stack exchange,提问作者antton Gorrochategui Aguirre
相关产品推荐
相关产品推荐

