Transport API

repository·master·Indexed 42 days ago

https://github.com/opendatach/transport

A public timetable data API for building web, desktop, or mobile applications. It can be used via the hosted service at transport.opendata.ch or self-hosted using PHP. The API provides functionality to find connections, search for locations, and retrieve station boards, with a PHP client library including entities for Connections, Journeys, Stations, and Addresses.

Tokens
7.4K
Snippets
34
Records
47
Agent score
53%

What's inside Transport API

  1. Install the Transport API

    master

    You can host the Transport API on your own server. To install it, clone the repository and use Composer to install dependencies. Ensure that the transport/var/ directory is writable by the web server.

    Note: Using the hosted version at transport.opendata.ch is recommended over self-hosting.

    $ git clone git://github.com/OpendataCH/Transport.git transport
    $ cd transport
    $ composer install
  2. Run the Transport API locally

    master

    To quickly get started with a local development setup, you can use PHP's built-in webserver. While not recommended for production, it is the easiest way to test locally.

    After starting the server, the API is accessible via the /web/api.php path.

    ```bash
    php -S localhost:8000

    Access the API at: http://localhost:8000/web/api.php

  3. The Stop entity represents a schedule checkpoint

    master
    The Transport\Entity\Schedule\Stop class is a data object representing a specific stop or checkpoint within a transport schedule. It contains information about the location (station), arrival and departure times (both as formatted strings and Unix timestamps), the platform, real-time availability, and delay information. It also includes a prognosis object to handle predicted vs. scheduled timing.
  4. The Journey entity represents a specific transportation section

    master

    The Transport\Entity\Schedule\Journey class is a data object representing a specific transportation section, such as a bus or a train traveling between stations. It contains metadata about the connection (name, category, operator, destination) and a list of checkpoints (stops) passed during the journey.

    Key properties include:

    • name: The name of the connection (e.g., 019351).
    • category: The type of connection (e.g., S).
    • subcategory: An internal category code.
    • categoryCode: An integer indicating the vehicle type:
      • 0, 1, 2, 3, 5, 8: train
      • 4: ship
      • 6: bus
      • 7: cable car (aerial, big)
      • 9: tram
    • number: The connection's line number (e.g., 13).
    • operator: The operator of the line (e.g., BBA).
    • to: The final destination of the line.
    • passList: An array of Transport\Entity\Schedule\Stop objects representing the checkpoints.
    • capacity1st: Maximum estimated occupation load of 1st class coaches.
    • capacity2nd: Maximum estimated occupation load of 2nd class coaches.
  5. Initialize the Statistics class

    master

    To use the Transport\Statistics class, you must instantiate it with a Predis\Client instance and a boolean $enabled flag. If $enabled is set to false, no data will be recorded in Redis, though some retrieval methods may still function depending on existing data.

    use Predis\Client;
    use Transport\Statistics;
    
    $redis = new Client();
    $stats = new Statistics($redis, true);
    $redis = new Client();
    $stats = new Statistics($redis, true);
  6. Configure the Transport API web service via Docker Compose

    master

    The web service runs the Transport API backend. It depends on a redis service and exposes the API on port 8000 of the host machine. You can configure the service behavior using the following environment variables:

    • REDIS_HOST: The hostname of the Redis instance (defaults to redis).
    • REDIS_PORT: The port for the Redis instance (defaults to 6379).
    • STATS_ENABLED: A boolean string ("true" or "false") to enable or disable statistics collection.
    • RATE_LIMITING_ENABLED: A boolean string ("true" or "false") to enable or disable rate limiting.
    services:
      web:
        build: .
        depends_on:
          - redis
        ports:
          - "8000:80"
        volumes:
          - .:/var/www/html
        environment:
          REDIS_HOST: redis
          REDIS_PORT: "6379"
          STATS_ENABLED: "true"
          RATE_LIMITING_ENABLED: "true"
      redis:
        image: redis
  7. Handle API errors and rate limits

    master

    The Transport\API class throws exceptions when encountering issues with the remote service:

    • Server Errors: Throws a generic \Exception if the server returns a 5xx error (includes status code and reason phrase).
    • Rate Limiting: Throws a Symfony\Component\HttpKernel\Exception\HttpException with status code 429 if the rate limit is exceeded.
    • Invalid JSON: Throws a \Exception if the response body cannot be decoded.
    • API-level Errors: Throws a \Exception if the JSON response contains an error field.