<!DOCTYPE html>
<html dir="ltr" lang="nl">
<head>
<meta http-equiv="refresh" content="60" />
<meta name="viewport" content="width=device-width,initial-scale=1.0;"/>
<link rel="stylesheet" type="text/css" href="index.css" />
<title>Komt de tram al? Of de bus?</title>
</head>
<body>
<?php
// Haal huidige datum en tijd op
$nu2 = date('H:i');
echo("<div class=\"hoelaat\"><em>Het is nu " . $nu2 . " uur</em></div>\n");
?>
<div class="schema">
<?php
// De lijninformatie wordt binnengehaald met curl
function ophalen($perron) {
$service_url = 'http://kv78turbo.ovapi.nl/tpc/' . $perron . '/departures';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
// Het JSON-bestand wordt omgezet naar een PHP array
$decoded = json_decode($curl_response,true);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('Er ging iets fout: ' . $decoded->response->errormessage);
}
// Hierin zit DE informatie
$alleinfo = $decoded[$perron];
// Uncomment de volgende regel als je de ruwe data wilt bekijken:
// var_export($decoded[$perron]);
return $alleinfo;
};
function toonZe($alleinfo, $richting) {
echo("<h1>Halte " . $alleinfo['Stop']['TimingPointName'] . " <em>" . $richting . "</em></h1><ul>");
$allevoertuigen = $alleinfo["Passes"];
// De array opnieuw sorteren op ExpectedArrivalTime
$tijdstippen = array();
foreach ($allevoertuigen as $voertuig) {
$tijdstippen[] = $voertuig['ExpectedArrivalTime'];
}
array_multisort($tijdstippen, SORT_ASC, $allevoertuigen);
foreach ($allevoertuigen as $row) {
// Deze gebruik je om de aankomsttijd weer te geven (nu niet in gebruik)
$tijd = date_parse($row['ExpectedArrivalTime']);
// Hiermee bereken je het aantal minuten
$rekentijd = strtotime($row['ExpectedArrivalTime']);
$nu = strtotime(date("Y-m-d\TH:i:s"));
if ($nu<$rekentijd) {
$verschil = round(abs($nu - $rekentijd) / 60,0);
};// Toon alle bussen die binnen 3 kwartier komen
if($verschil <= 900) {
if ($verschil == 1) {
echo("<li class=\"telaat\"><span>" .$row[LinePublicNumber] . "</span><h2>" . $row[DestinationName50] . "</h2><strong>NU!</strong></li>");
} else if ($verschil == 0) {
echo("<li class=\"bijnatelaat\"><span>" .$row[LinePublicNumber] . "</span><h2>" . $row[DestinationName50] . "</h2><strong>1 minuut</strong><strong>" . $row[MessageContent] . "</strong></li>\n");
} else {
echo("<li>" . $tijd[hour] . ":" . sprintf("%'.02d", $tijd[minute]) . " uur<em><span>" .$row[LinePublicNumber] . "</span><h2>" . $row[DestinationName50] . "</h2><h2>" . $row[SideCode] . "</h2><strong>(over " . $verschil . " minuten)</strong></li>\n");
};
};
};
echo("</ul>\n");
};
$alles = ophalen("37323910");
toonZe($alles, "heen");
?>
</div>
<div class="schema">
<?php
$alles = ophalen("37324580");
toonZe($alles, "terug");
?>
</div>
</body>
</html>