Skip to content

Instantly share code, notes, and snippets.

@yene
Created November 26, 2014 13:23
Show Gist options
  • Save yene/5b5c1e42006e19ce90f3 to your computer and use it in GitHub Desktop.
Save yene/5b5c1e42006e19ce90f3 to your computer and use it in GitHub Desktop.
show SBB delay on a hardcoded connection
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SBB Verspätungen</title>
<!-- Normalize.css is a customisable CSS file that makes browsers render all elements more consistently and in line with modern standards. -->
<link rel="stylesheet" media="screen" href="https://raw.github.com/necolas/normalize.css/master/normalize.css">
<style>
/* apply a natural box layout model to all elements http://paulirish.com/2012/box-sizing-border-box-ftw/ */
*, *:before, *:after { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
body {
/* best helvetica */
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
font-weight: 300;
}
</style>
</head>
<body>
<h1>SBB Verspätungen von Zürich Wollishofen</h1>
<?php
date_default_timezone_set('Europe/Zurich');
$url = "http://transport.opendata.ch/v1/connections";
$data = array("from" => "Zürich Wollishofen", "to" => "Zürich HB", "transportations[]" => "s_sn_r");
$query = $url . "?" . http_build_query($data);
$response = curl_request($query);
$a = json_decode($response, true);
$delays = array();
foreach ($a["connections"] as $key => $connection) {
$delay = $connection["from"]["delay"];
if ($delay !== null) {
$delays[] = $connection["from"];
}
}
if (count($delays) !== 0) {
echo "<ul>";
foreach ($delays as $from) {
echo "<li>";
$date = new DateTime($from["departure"]);
echo $date->format('H:i');
echo " hat " . $from["delay"] . " Minuten Verspätung.";
echo "</li>";
}
echo "</ul>";
}
/*
* source http://codular.com/curl-with-php
*/
function curl_request($url) {
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
return $resp;
}
?>
<footer>
<p>Disclaimer: Für verpasste Züge wird nicht gehaftet.</p>
</footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment