36534-vm/api/live_train_status_handler.php
2025-12-01 12:08:52 +00:00

48 lines
1.8 KiB
PHP

<?php
header('Content-Type: application/json');
require_once 'rail_api.php';
require_once __DIR__ . '/../db/config.php';
$trainNumber = $_GET['train_number'] ?? null;
$date = $_GET['date'] ?? null;
if (!$trainNumber || !$date) {
echo json_encode(['error' => 'Train number and date are required.']);
exit;
}
// For demo purposes, we will return a mock response that includes a known station code.
// In a real scenario, this would come from the RailApi call.
$mockApiResponse = [
'ResponseCode' => 200,
'TrainName' => 'SAMPARK KRANTI',
'TrainNo' => '12649',
'Position' => 'Arrived at KSR BENGALURU (SBC)',
'CurrentStation' => ['StationCode' => 'SBC', 'StationName' => 'KSR BENGALURU'],
'Route' => [
['StationCode' => 'NDLS', 'StationName' => 'NEW DELHI', 'ScheduleArrival' => '20:00', 'ActualArrival' => '20:00', 'ScheduleDeparture' => '20:15', 'ActualDeparture' => '20:15', 'IsCurrentStation' => false],
['StationCode' => 'SBC', 'StationName' => 'KSR BENGALURU', 'ScheduleArrival' => '06:30', 'ActualArrival' => '06:30', 'ScheduleDeparture' => '-', 'ActualDeparture' => '-', 'IsCurrentStation' => true]
]
];
if ($mockApiResponse['ResponseCode'] == 200) {
$currentStationCode = $mockApiResponse['CurrentStation']['StationCode'];
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT latitude, longitude FROM stations WHERE station_code = ?");
$stmt->execute([$currentStationCode]);
$coords = $stmt->fetch(PDO::FETCH_ASSOC);
if ($coords) {
$mockApiResponse['CurrentStation']['latitude'] = $coords['latitude'];
$mockApiResponse['CurrentStation']['longitude'] = $coords['longitude'];
}
} catch (PDOException $e) {
// Could not fetch coordinates, but we can still return the rest of the data.
}
}
echo json_encode($mockApiResponse);
?>