Skip to content
MartinLoeper edited this page Sep 30, 2014 · 10 revisions

KVV Basic API

1.) Get departure time of next public transport from 'Karlsruhe, Hauptbahnhof' to 'Karlsruhe, Durlacher Tor'

try {
	$efa = new \KVV\EFA();
	$trips = $efa->search('Karlsruhe Hauptbahnhof', 'Karlsruhe Durlacher Tor', time())->getFirstMatch()->getInfo();
	$most_recent_trip = $trips[0];
	$departure_time = $most_recent_trip->getInterval()[0];
	$arrival_time = $most_recent_trip->getInterval()[1];
        echo 'Next transport departs '.date("G:i", $departure_time ).' and arrives '.date("G:i", $arrival_time);
}
catch(\KVV\RouteNotFoundException $e) {
	echo "\n\nNo route found!\n\n";
}

2.) Instruct the user to make a more precise input

try {
	$efa = new \KVV\EFA();
	$tripRequest = $efa->search('Karlsruhe Hauptbahnhof', 'Karlsruhe Durlach', time());
    if($tripRequest->isCompleted()) {
		// do the work
	}
	else {
		$suggestionObj = $tripRequest->getInfo();
		echo "<pre>Your selection was not precise enough. Choose between one of the following options:\n\nOrigin:\n";
	        foreach($suggestionObj->getOriginSuggestions() AS $index=>$origin) {
			echo ($index+1).".) ".$origin."\n";
		}
		echo "\n\nDestination:\n";
			
		foreach($suggestionObj->getDestinationSuggestions() AS $index=>$destination) {
			echo ($index+1).".) ".$destination[1]."\n";
		}
			
		echo '</pre>';
	}
}
catch(\KVV\RouteNotFoundException $e) {
	echo "\n\nNo route found!\n\n";
}

As you can see, the destination 'Karlsruhe Durlach' is not unique, because it is the name of the region and a part of some station names! In this example, you have to provide some hyperlink or form, which sends the user selection back to the server to process the request with a fresh call to search(). For an example of that workflow, you can visit the KVV EFA (English).


3.) Show all trip information to the user

try {
	$efa = new \KVV\EFA();
	$trips = $efa->search('Karlsruhe Hbf', 'Karlsruhe Durlacher Tor', time(), \KVV\EFA::TRAVEL_TYPE_DEPARTURE, FALSE, FALSE, FALSE, FALSE, FALSE, \KVV\EFA::TRAVEL_CHANGE_SPEED_NORMAL, \KVV\EFA::LANGUAGE_ENGLISH)->getFirstMatch()->getInfo();
		echo '<pre>';	// we want to have a little bit structure here
	foreach($trips AS &$trip) {	// iterate through trips
		echo "<b>".$trip->getName()."</b>\n";
		echo "from: ".$trip->getRequestedOrigin()."\n";
		echo "to: ".$trip->getRequestedDestination()."\n";
		echo "Interval: ".date("G:i", $trip->getInterval()[0])." (".$trip->getInterval()[0].") - ".date("G:i", $trip->getInterval()[1])." (". $trip->getInterval()[1].")\n";
		echo "Duration: ".$trip->getDuration()."min\n";
		echo "with: ".implode(', ', $trip->getWith())."\n";
		echo "Changes: ".$trip->getChanges()."\n";
		$sections = &$trip->getSections();
		echo "Sections:\n";
		foreach($sections AS $index=>$section) {
			echo "\t".($index+1).") ".$section->getInfos()[0]." \n";	// more infos can be obtained from $section->getInfos() - just call count() to see if there is more...
			echo "\t\tfrom: ".$section->getOriginPlace()."\n";
			echo "\t\tto: ".$section->getDestinationPlace()."\n";
			echo "\t\tInterval: ".date("G:i", $section->getOriginTime())." - ".date("G:i", $section->getDestinationTime())."\n";
			echo "\t\tStations:";
			if(count($section->getStations()) > 0) {
				foreach($section->getStations() AS $index=>$station) {
				echo "\n\t\t\t(".($index+1).") ".$station->getName()." (".date("G:i", $station->getTime()).")\n";
				}
			}
			else {
				echo '-';
			}
		}
		echo "\n<hr>\n";
	}
	echo '</pre>';
}
catch(\KVV\RouteNotFoundException $e) {
	echo 'The requested station was not found!';
}

Output:

1. Journey
from: Karlsruhe Hbf
to: Karlsruhe Durlacher Tor
Interval: 1:18 (1412119080) - 1:33 (1412119980)
Duration: 15min
with:  S11 
Changes: 0
Sections:
	1) S-Bahn S11 
		from: Karlsruhe, Hbf Vorplatz
		to: Karlsruhe Kronenpl.(Erler-Str)
		Interval: 1:18 - 1:24
		Stations:
			(1) Karlsruhe Poststraße (1:19)

			(2) Karlsruhe Tivoli (1:21)

			(3) Karlsruhe Werderstraße (1:22)

			(4) Karlsruhe Rüppurrer Tor (1:23)
	2) Fußweg 
		from: Karlsruhe Kronenpl.(Erler-Str)
		to: Karlsruhe, Durlacher Tor
		Interval: 1:24 - 1:33
		Stations:-
[...]

KVV Live API

1.) If you are a student and you want to make your personal departure time board for the station in front of your house:

$efa = new \KVV\Live\EFA();
try {
	$query = 'Karlsruhe Poststraße';  // the searchphrase
	$stops = $efa->search($query);  // returns an array with possible stops
	$stop = \KVV\Live\EFA::getMostProbableMatch($stops, $query);  // auto select the most probable one
	echo $stop->getName()." (realtime):\n<pre>";
	foreach($efa->getDepartures($stop->getId()) AS &$departure) {  // get departures from this auto selected stop
		echo "[".$departure->getTime()."]\t".$departure->getRoute()." ".$departure->getDestination()."\n";
	}
	echo "</pre>";
}
catch(\KVV\Live\NoLiveDataAccessException $e) {
	echo "Invalid stop id or no live data access!";
}

Output:

Karlsruhe Poststraße (realtime):

[1 min]	S4 Bretten Gölshausen
[9 min]	S4 Hauptbahnhof
[02:10]	NL2 ZKM-Marktplatz
[02:20]	S1 Neureut
[02:24]	S1 Ettlingen
[02:48]	NL2 Tivoli

When you use this library on a Raspberry Pi with a small tft car monitor, you can accomplish your personal departure time board at home.

Clone this wiki locally