Skip to content

Instantly share code, notes, and snippets.

@Raven24
Created June 8, 2015 12:34
Show Gist options
  • Save Raven24/df85d12f17eba455db5b to your computer and use it in GitHub Desktop.
Save Raven24/df85d12f17eba455db5b to your computer and use it in GitHub Desktop.
www.bundesheer.at - Sperrgebiete API examples
<?php
/**
* Sperrzeiten JSON API Example
*
* @author Florian Staudacher, 2015
* <webteam@bmlvs.gv.at>
*/
$server_root = 'http://www.bundesheer.at';
$url = '/organisation/regional/noe/l75.php';
// $url = '/organisation/regional/ooe/molln_weg.php';
// $url = '/organisation/regional/stmk/seetaleralpe_wanderwege.php';
// $days = 7; // 1-14, default: 14
// fetch API data
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $server_root.$url.'?format=json&days='.$days,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 3,
CURLOPT_PROXY => '',
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_FORBID_REUSE => 1,
));
$output = curl_exec($c);
// curl error handling
if( $output === false ) {
printf(
'<strong>%s</strong><pre>%s</pre>',
curl_error($c),
print_r(curl_getinfo($c), true)
);
exit;
}
curl_close($c);
// parse JSON
$data = json_decode($output);
// could be more than one road...
$roads = is_array($data->data) ? $data->data : array($data->data);
$closing_times = $data->included;
// output
header('Content-Type: text/html; charset=utf-8');
header('Pragma: no-cache');
header('Cache-Control: private, no-cache, max-age=0, must-revalidate');
?>
<!DOCTYPE html>
<html>
<head>
<title>Sperrzeiten JSON API Example</title>
</head>
<body>
<?php
foreach($roads as $road)
{
echo '<div>';
echo '<strong>'.$road->attributes->name.'</strong>';
echo '<p>'.$road->attributes->description.'</p>';
// google maps iframe
// this is just an example, google doesn't always find the correct locations
echo '<iframe style="float:right; margin-right:2em;" class="mapframe" width="640" height="380"';
echo ' src="http://www.google.com/maps?output=embed&amp;f=d&amp;z=11&amp;saddr=';
echo urlencode($road->attributes->route[0].', Österreich');
echo '&amp;daddr=';
echo urlencode($road->attributes->route[1].', Österreich');
echo '"></iframe>';
// closing times list
echo '<dl>';
foreach($road->relationships->closing_times->data as $closed)
{
// PHP >= 5.3
$closed_info = array_pop(array_filter($closing_times, function($val) use ($closed) {
return ($val->type == $closed->type &&
$val->id == $closed->id);
}));
if( $last == $closed_info->attributes->date ) {
// same day
echo '<dd>und</dd>';
} else {
// new day
echo '<dt>'.date('D, d.m.Y', strtotime($closed_info->attributes->date)).'</dt>';
}
$int = explode('/', $closed_info->attributes->duration);
echo '<dd>Gesperrt von: '.date('H:i', strtotime(array_shift($int))).'</dd>';
echo '<dd>bis: '.date('H:i', strtotime(array_shift($int))).'</dd>';
$last = $closed_info->attributes->date;
}
echo '</dl>';
echo '</div>';
}
?>
</body>
<script>
// iframe caching bug in firefox, manual refresh
var frame = document.getElementsByClassName("mapframe");
for(var i=0; i<frame.length; i++) {
frame[i].contentWindow.location.href = frame[i].src;
}
</script>
</html>
<?php
/**
* Sperrzeiten XML API Example
*
* @author Florian Staudacher, 2015
* <webteam@bmlvs.gv.at>
*/
$server_root = 'http://www.bundesheer.at';
$url = '/organisation/regional/noe/l75.php';
// $url = '/organisation/regional/ooe/molln_weg.php';
// $url = '/organisation/regional/stmk/seetaleralpe_wanderwege.php';
// $days = 7; // 1-14, default: 14
// fetch API data
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $server_root.$url.'?format=xml&days='.$days,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 3,
CURLOPT_PROXY => '',
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_FORBID_REUSE => 1,
));
$output = curl_exec($c);
// curl error handling
if( $output === false ) {
printf(
'<strong>%s</strong><pre>%s</pre>',
curl_error($c),
print_r(curl_getinfo($c), true)
);
exit;
}
curl_close($c);
// parse XML
$data = new SimpleXMLElement($output);
$roads = $data->xpath('//road');
$closing_times = $data->xpath('//included/closing_time');
// output
header('Content-Type: text/html; charset=utf-8');
header('Pragma: no-cache');
header('Cache-Control: private, no-cache, max-age=0, must-revalidate');
?>
<!DOCTYPE html>
<html>
<head>
<title>Sperrzeiten XML API Example</title>
</head>
<body>
<?php
foreach($roads as $road)
{
echo '<div>';
echo '<strong>'.$road->attributes->name.'</strong>';
echo '<p>'.$road->attributes->description.'</p>';
// google maps iframe
// this is just an example, google doesn't always find the correct locations
echo '<iframe style="float:right; margin-right:2em;" class="mapframe" width="640" height="380"';
echo ' src="http://www.google.com/maps?output=embed&amp;f=d&amp;z=11&amp;saddr=';
echo urlencode(html_entity_decode($road->attributes->route->place[0]).', Österreich');
echo '&amp;daddr=';
echo urlencode(html_entity_decode($road->attributes->route->place[1]).', Österreich');
echo '"></iframe>';
// closing times list
echo '<dl>';
foreach($road->relationships->closing_times->data->closing_time as $closed)
{
// PHP >= 5.3
$closed_info = array_pop(array_filter($closing_times, function($val) use ($closed) {
return ($val->id->__toString() == $closed->id->__toString());
}));
if( $last == $closed_info->attributes->date->__toString() ) {
// same day
echo '<dd>und</dd>';
} else {
// new day
echo '<dt>'.date('D, d.m.Y', strtotime($closed_info->attributes->date)).'</dt>';
}
$int = explode('/', $closed_info->attributes->duration);
echo '<dd>Gesperrt von: '.date('H:i', strtotime(array_shift($int))).'</dd>';
echo '<dd>bis: '.date('H:i', strtotime(array_shift($int))).'</dd>';
$last = $closed_info->attributes->date;
}
echo '</dl>';
echo '</div>';
}
?>
</body>
<script>
// iframe caching bug in firefox, manual refresh
var frame = document.getElementsByClassName("mapframe");
for(var i=0; i<frame.length; i++) {
frame[i].contentWindow.location.href = frame[i].src;
}
</script>
</html>
@Raven24
Copy link
Author

Raven24 commented Jun 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment