Read RSS feed of website (blog) using php

This is tutorial about reading/fetching RSS feed of blog using simple php function, There is a function called simplexml_load_file(), By which we can easily render the xml file and read it into php array format. You must read the official document of Simple XML Parser . If you want to perform action on XML files

For fetching RSS feed of website you need to enable RSS feed first in your website after that, You’ll put RSS Feed URL in my php function which i am going to show you and see the result, Using below function you can easily add your blog articles into your website news ticker.



First create a simple RSS Feed reader function in php using using XML parser functions.

<?php
function getRssFeed($rssFeedUrl) {
  $rssFeed = array();	
  if(!empty($rssFeedUrl)) {
	foreach ($rssFeedUrl->channel->item as $feedItem) {
	$data['title'] = $feedItem->title;
	$data['pubDate'] = $feedItem->pubDate;
	$data['description'] = implode(' ', array_slice(explode(' ', $feedItem->description), 0, 40));
	array_push($rssFeed, $data);
	  }
   }
   return $rssFeed;
}
?>

After that call this function where you want to display your feeds. And design a simple layout for your feeds.

<div style="width:60%; margin:0 auto; border: 1px solid">
<h1>Rss Feed Using PHP</h1>
<div id="rss" style="padding-left:10px; padding-right:10px;">
<?php 
$rssFeedUrl = simplexml_load_file("http://www.iamrohit.in/feed/");
$rssResult = getRssFeed($rssFeedUrl);
foreach($rssResult as $rss) { 
?>
<br/>
<h4><?= $rss['title'] ?> </h4>
<p><?= $rss['description'] ?><br/>
<b><?= $rss['pubDate'] ?></b>
</p>
<?php } ?>
</div>	
</div>

See live demo and download source code.

DEMO

DOWNLOAD


If you like this post please don’t forget to subscribe my public notebook for more useful stuff

Posted in PHP