Real time info about all buses at a particular stop

Hello
I am quite new to the API world but I am trying to make a small web page which will render out in real time the bus services expected at a particular stop ID. I have been looking at the realtime API and am able to use CURL to request a dump of EVERYTHING, however I just want to query a particular stop.

For example
Query stop_id=123456
Results

SL3 - 4 minutes
651 - 6 minutes
288 - 23 minutes

I am not asking for someone to code this for me, but point me in the right direction of syntax to make this happen. I am reading through the various guides but if there are some ‘worked examples’ etc online that people could point me too it would be much appreciated.

Hi @iainbrew, have you had a look at the Trip Planner API? It has a ‘departure’ endpoint that will give you the functionality you’re looking for instead of sifting through the real-time API.

Thanks,
Alex

1 Like

Thanks so much Alex. Half way there - I was able to use your API demo’s to extract the required data. Next step for me is to then parse that so that I can display what I want (service name, departure time) and make it look nice on a display!

curl -X GET --header ‘Accept: application/json’ --header ‘Authorization: apikey ********’ ‘https://api.transport.nsw.gov.au/v1/tp/departure_mon?outputFormat=rapidJSON&coordOutputFormat=EPSG%3A4326&mode=direct&type_dm=stop&name_dm=211311&nameKey_dm=%24USEPOINT%24&departureMonitorMacro=true&excludedMeans=checkbox&exclMOT_1=1&exclMOT_4=1&exclMOT_7=1&exclMOT_9=1&exclMOT_11=1&TfNSWDM=true&version=10.2.1.42

I have used the guides and have tried to put together some PHP code from the Domestic Airport example t show realtime info of buses expected at a stop in Macquarie Park. Does the code look ok? In addition, as the API uses HTTPS, must my code also be run on an HTTPS server?

    <?php
$apiEndpoint = 'https://api.transport.nsw.gov.au/v1/tp/'; 
$apiCall = 'departure_mon';
$apiKey = 'my_key_goes_here' ;
	
// Set the location and time parameters
$when = time(); // Now
$stop = '201311'; // Macquarie Uni Bus Stop
	
// Build the request parameters
$params = array(
	'outputFormat' => 'rapidJSON', 
	'coordOutputFormat' => 'EPSG:4326',
	'mode' => 'direct',
	'type_dm' => 'stop',
	'name_dm' => $stop,
	'depArrMacro' => 'dep',
	'itdDate' => date('Ymd', $when), 
	'itdTime' => date('Hi', $when),
	'excludedMeans' => 'checkbox',
	'exclMOT_1' => '1',
	'exclMOT_4' => '1',
	'exclMOT_5' => '0',
	'exclMOT_7' => '1',
	'exclMOT_9' => '1',
	'exclMOT_11' => '1',
	'TfNSWDM' => 'true',
	'version' => '10.2.1.42'

);

$url = $apiEndpoint . $apiCall . $apiKey . ‘?’ . http_build_query($params);

// Perform the request and build the JSON response data
$response = file_get_contents($url);
$json = json_decode($response, true);
$stopEvents = $json[‘stopEvents’];

// Loop over returned stop events
foreach ($stopEvents as $stopEvent) {

// Extract the route information
$transportation = $stopEvent['transportation']; 
$routeNumber = $transportation['number']; 
$destination = $transportation['destination']['name'];
	
// In the case of a train, the location includes platform information
$location = $stopEvent['location'];

// Determine how many minutes until departure
$time = strtotime($stopEvent['departureTimePlanned']); 
$countdown = $time - time();
$minutes = round($countdown / 60);

// Output the stop event with a countdown timer
echo $minutes . "m from " . $location['disassembledName'] . "\n";
echo $routeNumber . " to " . $destination . "\n\n"; }

?>

1 Like

Just a quick note that we are making good progress with our little demo app for the campus. We have now added the ability for a QR code to be automatically generated which when scanned by a phone, will load a google map allowing the user to navigate to the bus stop to get to their service.

1 Like