Skip to content

Instantly share code, notes, and snippets.

@noestreich
Last active December 16, 2015 11:49
Show Gist options
  • Save noestreich/5430134 to your computer and use it in GitHub Desktop.
Save noestreich/5430134 to your computer and use it in GitHub Desktop.
BVG Departure-Times in Panic's Statusboard - Screenshot: http://i.imgur.com/29kQwt8.jpg PHP-snippet for Panic's iPad App "Statusboard" to show realtime "Ist-Abfahrtzeiten" from Berlins public transportation BVG. This PHP outputs the table of departuring trains on a predefined station, 8 Minutes from now (to account for the walk to the station). …
<?php
## set timezone
date_default_timezone_set('EUROPE/Berlin');
## set date and time + 8minutes walking distance to the train station
$zeit = date('H:i', strtotime("+8 minutes"));
$tag = date('d.m.y', strtotime("+8 minutes"));
## set trainstation ID (find ID here: http://mobil.bvg.de/Fahrinfo/bin/stboard.bin/dox?rt=0& ) and a transtation name or abbriviation as you like
$bahnhof ='9120001';
$bahnhofName ='Frankfurter Allee';
## get the BVG Ist-Abfahrtzeiten
$bvgURL = "http://mobil.bvg.de/Fahrinfo/bin/stboard.bin/dox?ld=0.1&rt=0&input=".$bahnhof."&boardType=dep&time=".$zeit."&productsFilter=11111111&date=".$tag."&maxJourneys=10&start=yes&";
$process = curl_init($bvgURL);
curl_setopt($process, CURLOPT_HEADER, false);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($process);
curl_close($process);
// find the table
preg_match("/<table.*?>.*?<\/[\s]*table>/s", $data, $table_html);
// find the rows
preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches);
// prettify rows
function showRow($reihe){
$rclean = str_replace('(Berlin)','',strip_tags($reihe,'<b><strong>'));
return $rclean;
}
?>
<table>
<tr>
<td style="font-size:22px;"><strong> Abfahrt / Richtung ab <?php echo $bahnhofName; ?></strong></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][1]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][2]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][3]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][4]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][5]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][6]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][7]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][8]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][9]); ?></td>
</tr>
<tr>
<td><?php echo showRow($matches[0][10]); ?></td>
</tr>
</table>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<meta http-equiv="Cache-control" content="no-cache" />
<script type="text/javascript">
/* function updates the "output" element with the result from the
"/update" url then calls itself again in 4 seconds
*/
function updater(){
var request = createXMLHttpRequest();
// add time to GET request to prevent caching issues
// EDIT NEXT LINE to replace EXAMPLE.com
request.open("GET", "http://www.EXAMPLE.com/bvg-table.php?" + (new Date()).getTime(), true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
document.getElementById("output").innerHTML = request.responseText ;
setTimeout("updater()",4000);
}
}
request.send(null);
}
/* cross browser way of creating XMLHttpRequests */
function createXMLHttpRequest() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined") {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
throw new Error("XMLHttpRequest not supported");
}
}
</script>
</head>
<body onload="updater()">
<div id="output"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment