Obs: Detta innehåll finns inte tillgängligt på svenska. Därför ser du engelska versionen. Om du tycker att denna sida borde översättas till svenska, kan du skriva till oss på support.trafiklab.se.

Om du vill se webbsidan på engelska, klicka här.

We created a PHP SDK to easily use the ResRobot APIs. This way you don’t need to worry about making requests, caching, or parsing responses. All responses are parsed and returned as PHP classes.

Installation

Installing can be done by using Composer:

composer require trafiklab/resrobot-php-sdk

Requirements

The following software is required to use this SDK:

  • PHP 7.1 or higher
  • PHP Curl extension
  • PHP JSON extension

Usage

In order to use the ResRobot Timetable and Routeplanning APIs, you need to obtain an API key from Trafiklab first.

Getting Timetables (departures or arrivals from a stop)

Timetables

Request

The following code example illustrates how you can retrieve a timetable for a certain stop.

 1$wrapper = new ResRobotWrapper();
 2
 3// Create a new routeplanning object. The wrapper will instantiate an object of the interface type.
 4$wrapper = $wrapper->createTimeTableRequestObject();
 5$wrapper->setStopId("740000001");
 6$wrapper->setTimeTableType(TimeTableType::DEPARTURES);
 7
 8$wrapper->setUserAgent("<YOUR_USER_AGENT>");
 9$wrapper->setTimeTablesApiKey("<YOUR_API_KEY>");
10$response = $resRobotWrapper->getTimeTable($departuresRequest);

<YOUR_API_KEY> is obtained from Trafiklab. <YOUR_USER_AGENT> is a string which identifies your application. While this is not enforced in any way, it is good practice to use a clear user agent. An example could be MyDemoApp/1.0.0 ([email protected]). If you don’t want to send a user agent, you can just leave out this line.

Detailed information about ResRobot request parameters can be found at the ResRobot departures/arrivals API page. Only the most important/most used request parameters are implemented in the SDK, in order to reduce clutter, and to ensure that we can keep the SDK unchanged in case of changes to the API. If you believe we have missed an important field, please create an issue so we can review this.

Response

In order to use the data returned by your request, you can simply call getTimeTable() on the response object. This method returns an array of TimeTableEntry instances, each of which describes one departure or arrival. You can look at the code and PHPDoc in order to get up-to-date information on which fields are available. Detailed information about ResRobot responses can be found at the ResRobot departures/arrivals API page.

The following code gives a quick idea on how the SDK is used.

 1$entry = $response->getTimetable()[0]; // Get the first result
 2// Type of transport, one of the constants in Trafiklab\Common\Model\Enum\TransportType
 3$entry->getTransportType();
 4// The name of the stop location
 5$stop = $timeTableEntry->getStopName()
 6// The number of the line
 7$lineNumber = $timeTableEntry->getLineNumber();
 8// The direction of the vehicle
 9$direction = $timeTableEntry->getDirection();
10// The scheduled departure time at the stop
11$scheduledStopTime = $timeTableEntry->getScheduledStopTime();

Routeplanning

Request

The following code example illustrates how you can plan a route from A to B

 1$queryTime = new DateTime();
 2$queryTime->setTime(18, 0);
 3
 4$wrapper = new ResRobotWrapper();
 5
 6// Create a new routeplanning object. The wrapper will instantiate an object of the interface type.
 7$wrapper = $wrapper->createRoutePlanningRequestObject();
 8$wrapper->setOriginId("740000001");
 9$wrapper->setDestinationId("740000002");
10$wrapper->setDateTime($queryTime);
11
12$wrapper->setUserAgent(("<YOUR_USER_AGENT>");
13$wrapper->setRoutePlanningApiKey("<YOUR_API_KEY>");
14$response = $resRobotWrapper->getRoutePlanning($routePlanningRequest);

Response

In order to use the data returned by your request, you can simply call getTrips() on the response object. This method returns an array of Trip instances, each of which describes one departure or arrival. You can look at the code and PHPDoc in order to get up-to-date information on which fields are available. Detailed information about ResRobot responses can be found at the ResRobot departures/arrivals API page.

The following code gives a quick idea on how the SDK is used.

 1$trip = $response->getTrips()[0]; // Get the first result
 2
 3// Tell the user about every leg in their journey.
 4foreach ($trip->getLegs() as $leg) {
 5
 6   // There are two types of legs (at this moment): Vehicle journeys, where a vehicle is used, or walking parts
 7   // where a user walks between two stations. Not all fields are available for walking parts, so we need to handle them differently.
 8
 9   if ($leg->getType() == RoutePlanningLegType::VEHICLE_JOURNEY) {
10       $leg->getVehicle()->getType();
11       $leg->getVehicle()->getNumber();
12       $leg->getDirection();
13       $leg->getDeparture()->getStopName();
14       $leg->getDeparture()->getScheduledDepartureTime()->format("H:i");
15       $leg->getDeparture()->getScheduledDepartureTime()->getTimestamp();
16       $leg->getArrival()->getScheduledArrivalTime()->getTimestamp();
17       $leg->getArrival()->getStopName();
18       // More fields are available
19   } else if ($leg->getType() == RoutePlanningLegType::WALKING) {
20       // Limited fields when walking!
21       $leg->getDeparture()->getStopName(); // origin
22       $leg->getArrival()->getStopName(); // destination
23   }
24}