Display RSS feed as alert using PHP

Our school district had a need where we wanted to have the no school notice show on all of our district pages when an alert is sent out.  I wanted to automate the process so that there wouldn’t need to be any intervention last minute.  The alert system we use, Alert Solutions, has an option to output to an RSS feed.  By using this and a small amount of PHP, I was able to get the alerts to show up on the site automatically and display for 18 hours without any special intervention other than when the admin makes the notification, they have to remember to check the “RSS” box as well. 

The issue became that the RSS feed never lets go of any posts, and on top of that, it outputs the oldest at the top.  To fix this, we look for the number of keys, then pull only the latest key.  This means that we are only looking at the most recent entry.  If it is less than 18 hours old, it is displayed automatically.  Otherwise the div is set to display:none; and doesn’t show.

Below is the code I used.


<html>
<head>
<link rel="stylesheet" href="/alert-rss.css" media="all">
</head>

<body>

<?php
	//Set timezone and load array with only the fields we need
	date_default_timezone_set('America/New_York');
	$rss = new DOMDocument();
	$rss->load('https://url-of-rss-feed.com');
	$feed = array();
	foreach ($rss->getElementsByTagName('item') as $node) {
		$item = array ( 
			'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
			'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
			'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
			);
		array_push($feed, $item);
	}
	
	
	//Find the last entry array key so we only display current RSS feed
	end($feed);
	$key = key($feed);
	
	//Set variables to pull appropriate data from array
	$title = str_replace(' & ', ' &amp; ', $feed[$key]['title']);
	$description = $feed[$key]['desc'];
	$date = date('l F d, Y H:ma', strtotime($feed[$key]['date']));

	//Set $end_date for when RSS expires.  Calculation entered as (hours*secondsperhour).
	$end_date = date('l F d, Y H:ma', strtotime($feed[$key]['date']) + (18*3600));

	//Set how today is calculated for comparison purposes
	$today = date('l F d, Y H:ma');

	//Display what we have found if it is current
	if($today < $end_date) {
		echo '<div id="blinky-div"><p><strong>'.$title.'</strong><br />';
		echo '<small><em>Posted on '.$date.'</em></small></p>';
		echo '<p>'.$description.'</p></div>';
	//Hide DIV if there is no current data
	} else {
		    echo '<div style="display: none;">No current RSS</div>';
	}

?>

</body>
</html>

 

And here is the CSS I used:

@keyframes blink { 
    50% {
   	border-color: #ff0000; 
   	border-style: solid;
   	border-width: 4px;
   	}
   	0% {
   	border-color: #fff; 
   	border-style: solid;
   	border-width: 4px;
   	} 
}
#blinky-div {
    animation-name: blink ;
    animation-duration: .5s ;
    animation-timing-function: step-end ;
    animation-iteration-count: infinite ;
    animation-direction: alternate ;
}
This entry was posted in Computers and Hardware, Technology, Web/Internet. Bookmark the permalink.

Comments are closed.