/**
* Gets departures from the given station starting at the given time.
*
* @param int $stationID
* @param Carbon $time
* @return array
* @throws ApiException
*/
public static function getDepartures(int $stationID, Carbon $time, int $maxJourneys = 10)
{
// prepare parameters for our request
$query = ['input' => $stationID, 'boardType' => 'dep', 'time' => $time->format('H:i'), 'date' => $time->format('d.m.y'), 'maxJourneys' => $maxJourneys, 'start' => 'yes'];
// send it to the bvg mobile site
$response = \Requests::get(self::getApiEndpoint() . '?' . http_build_query($query));
if ($response->status_code == 200) {
// our results array
$departures = [];
// prepare document
$dom = new Dom();
$dom->load($response->body);
// get date from API
$date = $dom->find('#ivu_overview_input');
$date = trim(substr($date->text, strpos($date->text, ':') + 1));
$date = Carbon::createFromFormat('d.m.y', $date, 'Europe/Berlin');
// get table data without the first line (header)
$rows = $dom->find('.ivu_result_box .ivu_table tbody tr');
// loop through each departure in the table
foreach ($rows as $row) {
// get columns
$columns = $row->find('td');
// explode time into two parts
$time = explode(':', strip_tags($columns[0]));
// push the departure onto our results array
$departures[] = ['time' => $date->copy()->hour($time[0])->minute($time[1])->second(0), 'line' => trim(strip_tags($columns[1]->find('a')[0])), 'direction' => trim(strip_tags($columns[2]))];
}
// return results
return $departures;
} else {
throw new ApiException('Failed getting station data from BVG API');
}
}