Enable API Statistics via Redis
masterconfig.php file using the $redis variable. Once configured, statistics can be viewed at the stats.php endpoint.repository·master·Indexed 42 days ago
https://github.com/opendatach/transportA 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.
config.php file using the $redis variable. Once configured, statistics can be viewed at the stats.php endpoint.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 installTo 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:8000Access the API at: http://localhost:8000/web/api.php
Connection object represents a possible journey between two locations. It encapsulates the departure and arrival points, the total duration, the number of transfers, service regularity, transport products, and the individual sections that make up the journey.To customize the API behavior, create a config.php file by copying the provided sample file. You can then override specific variables within this file to suit your environment.
cp config.php.sample config.phpTransport\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.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: train4: ship6: bus7: cable car (aerial, big)9: tramnumber: 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.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);If composer install fails with a requirement resolution error on Ubuntu, it is likely because the php-xml package is missing. Install it using apt.
sudo apt install php-xmlThe 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: redisCoordinate::createFromXml static method to instantiate a Coordinate object from a `\The Transport\API class throws exceptions when encountering issues with the remote service:
\Exception if the server returns a 5xx error (includes status code and reason phrase).Symfony\Component\HttpKernel\Exception\HttpException with status code 429 if the rate limit is exceeded.\Exception if the response body cannot be decoded.\Exception if the JSON response contains an error field.