-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpark-news-flash-briefing-last-month.php
89 lines (78 loc) · 2.63 KB
/
park-news-flash-briefing-last-month.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
87
88
89
<?php
if(isset($_GET['park'])){
$park = $_GET['park'];
$dataURL = "https://developer.nps.gov/api/v0/newsreleases?parkCode=$park";
}else {
$park = 'ACAD';
$dataURL = 'https://developer.nps.gov/api/v0/newsreleases?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
$newsTotal = $json->total;
$currentNewsItems = 0;
$newsCount = 0;
$currentTime = time();
$fourWeeksAgo = $currentTime - (60 * 60 * 24 * 7 * 4);
// Function to write json for each news item
function outputNews() {
global $currentNewsItems, $newsCount, $fourWeeksAgo, $json;
for ($i = 0; $i < $currentNewsItems; $i++) {
if (strtotime($json->data[$i]->releaseDate) >= $fourWeeksAgo) {
$newsCount = $newsCount + 1;
echo '{';
echo '"uid": "', $json->data[$i]->id, '",';
echo '"updateDate": "', gmdate("Y-m-d\TH:i:s\Z", strtotime($json->data[$i]->releaseDate)), '",';
echo '"titleText": ', json_encode($json->data[$i]->title), ',';
if ($json->data[$i]->url != '') {
echo '"mainText": ', json_encode($json->data[$i]->abstract), ',';
echo '"redirectionUrl": "', $json->data[$i]->url, '"';
} else {
echo '"mainText": ', json_encode($json->data[$i]->abstract);
}
if ($currentNewsItems > $newsCount) {
echo '},';
} else {
echo '}';
}
}
}
}
for ($i = 0; $i < count($json->data); $i++) {
if (strtotime($json->data[$i]->releaseDate) >= $fourWeeksAgo) {
$currentNewsItems = $currentNewsItems + 1;
}
}
if ($currentNewsItems > 0) {
// Start outputting json if there are news items
header('Content-Type: application/json');
echo '[';
// Output news items
outputNews();
echo ']';
} else {
// Start outputting json if there are no news items
header('Content-Type: application/json');
echo '[';
echo '{';
echo '"uid": "', uniqid('',true), '",';
echo '"updateDate": "', $isoDate, '",';
echo '"titleText": "No news.",';
echo '"mainText": "There are no recent news items at this time. Please visit the park website for information about news and events.",';
echo '"redirectionUrl": "https://www.nps.gov/', $park, '/news.htm"';
echo '}';
echo ']';
}
?>