Hi eveyone, hoping that someone can help this first time GTFS user on the Trip planner example here:
I have copied the full example PHP from the bottom of the page, and getting the following when running from http://localhost:8888/gtfs/test.php.
Warning : file_get_contents(https://api.transport.nsw.gov.au/v1/tp/departure_mon?outputFormat=rapidJSON&coordOutputFormat=EPSG%3A4326&mode=direct&type_dm=stop&name_dm=10101331&depArrMacro=dep&itdDate=20190524&itdTime=1211&TfNSWDM=true): failed to open stream: Operation timed out in /Applications/MAMP/htdocs/gtfs/test.php on line 21
Warning : Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/gtfs/test.php on line 26
Does anyone have a working example I could use, or PLEASE point me in the direction of what I am doing wrong. (PS I am using my API Key not the XXXX).
Many thanks
Hi @Scout.Iris, welcome to our forum!
Hopefully a developer in our community will help you out with your issue. Since we can’t access your local files, it might be a good idea to post a full sample of your code so others can see it, after removing your API key of course!
Thanks!
Thankyou so much for your help and assistance. The code below is straight from the example project, appreciate any help you can give me as Id love to be tinkering with this over the weekend.
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
date_default_timezone_set("Australia/Sydney");
$apiEndpoint = 'https://api.transport.nsw.gov.au/v1/tp/';
$apiCall = 'departure_mon'; // Set the location and time parameters
$when = time(); // Now
$stop = '10101331'; // Domestic Airport Station // 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), 'TfNSWDM' => 'true' );
$url = $apiEndpoint . $apiCall . '?' . http_build_query($params);
// Create a stream
$opts = [
"http" => [
"method" => "GET",
"header" => "Authorization: apikey XXXXXX API KEY XXXXXX\r\n"
]
];
// Perform the request and build the JSON response data
$context = stream_context_create($opts);
$response = file_get_contents($url, false, $context);
$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['name'] . "\n<br />";
echo $routeNumber . " to " . $destination . "\n\n<br /><br />";
}
echo "end";
?>
No reason to respond, I have managed to work out the problem. My office firewall was preventing the call from executing and returning a feed.
Running from outside the firewall was successful.
Cheers
1 Like