Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

How to check empty seats on a train using the IRCTC API (or any Indian Railway API)

To check empty seats on a train using the IRCTC API (or any Indian Railway API), follow these steps:

 

Steps to Get Seat Availability:

  1. Register for API Access:
    Get access to an official railway API provider such as:

  2. Set Up API Integration:
    Obtain your API key after registration. This key is required to authenticate your requests.

  3. Use the Seat Availability Endpoint:
    The specific endpoint for checking seat availability will usually require the following parameters:

    • Train Number
    • Source Station Code
    • Destination Station Code
    • Date of Journey
    • Class of Travel (e.g., Sleeper, 3AC, 2AC)
    • Quota (e.g., General, Tatkal)
  4. Example API Call Structure:

    GET https://api.example.com/v2/seat_availability/train/{train_number}/source/{source_code}/destination/{destination_code}/date/{date}/class/{class}/quota/{quota}
    Headers:
     Authorization: Bearer 
    
  5. Parse the Response:
    The response will typically include details about seat availability for the requested class and date. It will also show the booking status for subsequent days if the train runs daily.

Example Data Fields in Response:

  • Train Number:
    12345
  • Class:
    Sleeper (SL)
  • Availability: Available/Waitlist
  • Date: Specific journey date
  • Quota: General/Tatkal/Ladies

Things to Note:

  • Dynamic Availability: Seats availability changes frequently, so you'll need to fetch real-time data.
  • Quota: Quotas (like Tatkal or Senior Citizen) may affect seat availability.
  • Data Limitations: Ensure your API plan covers enough API calls for your needs.

Resources:

Here’s how you can create a complete PHP program to check seat availability using an API like RailAPI or IRCTC:


Prerequisites

  1. API Registration: Obtain an API key from a service like RailwayAPI or a similar provider.
  2. PHP Setup: Ensure you have a PHP environment running (e.g., XAMPP, WAMP, or a shared hosting server).
  3. Install cURL: Ensure cURL is enabled in your PHP configuration (most PHP setups have it enabled by default).

Complete PHP Program

// API configuration
$api_url = "https://api.railwayapi.com/v2/check-seat/train/"; // Replace with your provider's API endpoint
$api_key = "your_api_key_here"; // Replace with your API key

// Parameters (example values)
$train_number = "12345"; // Replace with the train number
$source_code = "NDLS"; // Replace with the source station code
$destination_code = "BCT"; // Replace with the destination station code
$journey_date = "2024-12-25"; // Replace with the journey date (YYYY-MM-DD)
$class = "SL"; // Replace with the class (e.g., SL, 3A, 2A, CC)
$quota = "GN"; // Replace with the quota (e.g., GN for General)

// Construct the API URL
$request_url = "{$api_url}{$train_number}/source/{$source_code}/dest/{$destination_code}/date/{$journey_date}/class/{$class}/quota/{$quota}/apikey/{$api_key}";

// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the API call
$response = curl_exec($ch);

// Handle cURL errors
if ($response === false) {
    echo "cURL Error: " . curl_error($ch);
    exit;
}

// Close cURL session
curl_close($ch);

// Parse the response
$data = json_decode($response, true);

// Check if the response is valid
if (isset($data['availability'])) {
    echo "Seat Availability for Train {$train_number}:n";
    foreach ($data['availability'] as $day) {
        echo "Date: " . $day['date'] . " - Status: " . $day['status'] . "n";
    }
} else {
    echo "Error fetching seat availability: " . $data['error'] ?? "Unknown error";
}
?>

Explanation of the Code

  1. API URL Construction:
    Replace the placeholders with actual values like train number, station codes, and date.

  2. cURL Setup:
    The cURL library is used to make the API request.

  3. Error Handling:
    The program checks if the response is valid or if there are errors.

  4. Response Parsing:
    The JSON response is decoded and parsed to display seat availability.


How to Run

  1. Save the code as
    seat_availability.php
    .
  2. Run it on a local server (XAMPP, WAMP) or upload it to your hosting server.
  3. Access it through a browser or terminal.

Expected Output

For a valid request, you'll see something like:

Seat Availability for Train 12345:
Date: 2024-12-25 - Status: AVAILABLE 10
Date: 2024-12-26 - Status: RAC 5
Date: 2024-12-27 - Status: WL 20

 

caa December 20 2024 7 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 1
Members Online 0

Total Members: 11
Newest Member: Jhilam