Skip to content

Instantly share code, notes, and snippets.

@narendrans
Created April 17, 2014 21:32
Show Gist options
  • Save narendrans/11012811 to your computer and use it in GitHub Desktop.
Save narendrans/11012811 to your computer and use it in GitHub Desktop.
Parses the feed and displays the title, author, published date etc in html. All parameters are optional except feed_url
<?php
/**
* This script gets the feeds of the specified rss link.
*
* @author - Naren
* @version - 1.0.0
* @since - 4th April, 2014. 17:23 EST
*
*/
/*
Parameters:
feed_url required - The url of the rss
limit optional - Number of posts to be displayed
getDate optional - If given the date is displayed
getAuthor optional - If give the author is displayed
getDescription optional - If given the description is displayed
numberOfWords optional - If given the number of words to be displayed will be used
Examples:
1. To display the title of the latest post only
getFeeds.php?
feed_url=http://narendran.info/blog/?feed=rss2
2. To display the tile of latest 3 posts
getFeeds.php?
feed_url=http://narendran.info/blog/?feed=rss2
&limit=3
3. To display the title and description of latest 3 posts
getFeeds.php?
feed_url=http://narendran.info/blog/?feed=rss2
&limit=3
&getDescription
4. To display the title, author, date and first 20 words of description of latest 3 posts
getFeeds.php?
feed_url=http://narendran.info/blog/?feed=rss2
&limit=3
&getAuthor
&getDate
&getDescription
&numberOfWords=20
*/
$link = $_GET['feed_url'];
$limit = 1;
$getDate = false;
$getAuthor = false;
$getDescription = false;
$numberOfWords = 10;
if (!isset($_GET['feed_url'])) {
echo "You must specify the link atleast!";
exit;
}
if (isset($_GET['limit']))
$limit = $_GET['limit'];
if (isset($_GET['getDate']))
$getDate = true;
if (isset($_GET['getAuthor']))
$getAuthor = true;
if (isset($_GET['getDescription']))
$getDescription = true;
if (isset($_GET['numberOfWords']))
$numberOfWords = $_GET['numberOfWords'];
$rss = new DOMDocument();
$rss->load($link);
$feed = array();
$count = 0;
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'description' => word_trim($node->getElementsByTagName('description')->item(0)->nodeValue, $numberOfWords, true),
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'author' => $node->getElementsByTagName('creator')->item(0)->nodeValue
);
if ($count >= $limit)
break;
else
$count++;
array_push($feed, $item);
}
for ($i = 0; $i < count($feed); $i++) {
echo "<span class='post'>";
echo "<span class = 'rsstitle'><a title='" . str_replace(' & ', ' &amp; ', $feed[0]['title']) . "' href='" . $feed[$i]['link'] . "'>" . $feed[$i]['title'] . "</a></rsstitle>";
if ($getDate)
echo " - <span class='rssdate'>" . substr($feed[$i]['date'], 0, -15) . "</span>";
if ($getAuthor)
echo " - <span class='rssauthor'>" . $feed[$i]['author'] . "</span>";
if ($limit == 1) {
echo "</span>";
break;
}
echo "<br/>";
if ($getDescription)
echo "<span class='rssdescription'>" . $feed[$i]['description'] . "</span>";
echo "<br/>";
echo "</span>";
}
function word_trim($string, $count, $ellipsis = FALSE)
{
$words = explode(' ', $string);
if (count($words) > $count) {
array_splice($words, $count);
$string = implode(' ', $words);
if (is_string($ellipsis)) {
$string .= $ellipsis;
} elseif ($ellipsis) {
$string .= '&hellip;';
}
}
return $string;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment