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"; }
?>