How to fetch RSS feed using PHP
Posted on August 2nd in PHP Bites
This one is a very simple tutorial which will guide you to insert RSS feed from another website into your website using very little PHP. As you know, PHP 5 introduced the DOM extension to work with XML documents. We will use this DOM extension to do this simple PHP operation.
Check the below simple PHP code. You just need to change the feed URL at code line 3 ( just use your desired link) and the number of posts to display which is in line 14. We used here our WordPress blog feed to show you an example.
<br />
<?php<br />
$rss = new DOMDocument();<br />
$rss->load('http://techblog.magicnines.com/feed/');<br />
$feed = array();<br />
foreach ($rss->getElementsByTagName('item') as $node) {<br />
$item = array (<br />
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,<br />
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,<br />
);<br />
array_push($feed, $item);<br />
}<br />
$limit = 5;<br />
for($x=0;$x<$limit;$x++) {<br />
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);<br />
$link = $feed[$x]['link'];<br />
echo '<li><a href="'.$link.'" title="'.$title.'">'.$title.'</a></li>';<br />
}<br />
?><br />
It is even simpler than you thought.





