Created
June 8, 2015 12:34
-
-
Save Raven24/df85d12f17eba455db5b to your computer and use it in GitHub Desktop.
www.bundesheer.at - Sperrgebiete API examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sperrzeiten JSON API Example | |
* | |
* @author Florian Staudacher, 2015 | |
* <[email protected]> | |
*/ | |
$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&f=d&z=11&saddr='; | |
echo urlencode($road->attributes->route[0].', Österreich'); | |
echo '&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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sperrzeiten XML API Example | |
* | |
* @author Florian Staudacher, 2015 | |
* <[email protected]> | |
*/ | |
$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&f=d&z=11&saddr='; | |
echo urlencode(html_entity_decode($road->attributes->route->place[0]).', Österreich'); | |
echo '&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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
offizielle Version: https://gist.github.com/webteam-bundesheer/4574fe3e39ab8ca2ebef