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

46 lines
1.6 KiB
PHP

<?php
class RailApi {
private $apiKey;
private $baseUrl = 'https://indianrailapi.com/api/v2/';
public function __construct() {
// IMPORTANT: The API key is a placeholder. Replace it with your actual key.
$this->apiKey = getenv('RAIL_API_KEY') ?: 'YOUR_API_KEY_HERE';
}
private function makeRequest($endpoint) {
$url = $this->baseUrl . $endpoint;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
public function checkPnrStatus($pnrNumber) {
$endpoint = "PNRCheck/apikey/{$this->apiKey}/PNRNumber/{$pnrNumber}/";
return $this->makeRequest($endpoint);
}
public function getLiveTrainStatus($trainNumber, $date) {
$endpoint = "livetrainstatus/apikey/{$this->apiKey}/trainnumber/{$trainNumber}/date/{$date}/";
return $this->makeRequest($endpoint);
}
public function getLiveStation($stationCode, $hours) {
$endpoint = "LiveStation/apikey/{$this->apiKey}/StationCode/{$stationCode}/hours/{$hours}/";
return $this->makeRequest($endpoint);
}
public function searchTrains($from, $to, $date) {
// Note: The API docs you provided don't have a direct train search endpoint like this.
// This is a placeholder for a possible endpoint or a feature that might need a different API.
// For now, it will return a mock response.
return ['error' => 'Train search functionality is not available in the provided API endpoints.'];
}
}
?>