Created
October 7, 2014 17:55
-
-
Save sleslie321/a5d09126f441af5871b6 to your computer and use it in GitHub Desktop.
Consume RSS feed with php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(' & ', ' & ', $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