-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpark-alert-flash-briefing.php
86 lines (77 loc) · 2.52 KB
/
park-alert-flash-briefing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
if(isset($_GET['park'])){
$park = $_GET['park'];
$dataURL = "https://developer.nps.gov/api/v0/alerts?parkCode=$park";
}else {
$park = 'ACAD';
$dataURL = 'https://developer.nps.gov/api/v0/alerts?parkCode=acad';
}
// Get cURL resource
$curl = curl_init();
// Set options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $dataURL,
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
CURLOPT_HTTPHEADER => array('Authorization: INSERT-API-KEY-HERE')
));
// Send the request & save response to $response
$response = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$json = json_decode($response);
// Set variables
$alertsTotal = $json->total;
$alertsCount = 0;
$isoDate = gmdate("Y-m-d\TH:i:s\Z");
// Function to write json for each alert type
function outputAlert($alertType) {
global $alertsTotal, $alertsCount, $json, $isoDate;
for ($i = 0; $i < count($json->data); $i++) {
if ($json->data[$i]->category == $alertType) {
$alertsCount = $alertsCount + 1;
echo '{';
echo '"uid": "', $json->data[$i]->id, '",';
echo '"updateDate": "', $isoDate, '",';
echo '"titleText": ', json_encode($alertType . ' Alert: ' . $json->data[$i]->title), ',';
if ($json->data[$i]->url != '') {
echo '"mainText": ', json_encode($json->data[$i]->description), ',';
echo '"redirectionUrl": "', $json->data[$i]->url, '"';
} else {
echo '"mainText": ', json_encode($json->data[$i]->description);
}
if ($alertsTotal > $alertsCount) {
echo '},';
} else {
echo '}';
}
}
}
}
if ($alertsTotal > 0) {
// Start outputting json if there are alerts
header('Content-Type: application/json');
echo '[';
// Output danger alerts
outputAlert("Danger");
// Output park closure alerts
outputAlert("Park Closure");
// Output caution alerts
outputAlert("Caution");
// Output information alerts
outputAlert("Information");
echo ']';
} else {
// Start outputting json if there are no alerts
header('Content-Type: application/json');
echo '[';
echo '{';
echo '"uid": "', uniqid('',true), '",';
echo '"updateDate": "', $isoDate, '",';
echo '"titleText": "No active alerts.",';
echo '"mainText": "There are no active alerts at this time. Please visit the website for information about news and events.",';
echo '"redirectionUrl": "https://www.nps.gov/', $park, '"';
echo '}';
echo ']';
}
?>