Skip to content

Instantly share code, notes, and snippets.

@sleslie321
Created October 7, 2014 17:55
Show Gist options
  • Save sleslie321/a5d09126f441af5871b6 to your computer and use it in GitHub Desktop.
Save sleslie321/a5d09126f441af5871b6 to your computer and use it in GitHub Desktop.
Consume RSS feed with php
<?php
$rss = new DOMDocument();
$rss->load('http://feeds.bbci.co.uk/news/rss.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p><br/><br/>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment