Import RSS Feeds from any rss enabled site using jFeed (jQuery)
This simple tutorial will show u how to Import rss feeds of any site into your own custom area/block of site. It can be used as news import from yahoo, bbc etc. As we know, RSS feeds usually using to transfer some news to readers. Today I will tell you about RSS feeds and teach you how to make own RSS aggregator. So you will able to display different RSS feeds at your website with nice design.
Here is a sample:
Live Demo
Download in package
Ok, download the example files and lets start coding !
Step 1. HTML
As usual, we start with the HTML.
This is our main page code (with 2 RSS blocks). Pay heed to RSSAggrCont elements. At its params. ‘rssnum’ mean how many rss elements will display at page. ‘rss_url’ – RSS url. Hope you will able to easy change these params.
templates/rss_page.html
<link href="templates/css/styles.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.jfeed.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.aRSSFeed.js"></script>
<div class="RSSAggrCont" rssnum="5" rss_url="http://rss.news.yahoo.com/rss/topstories">
<div class="loading_rss">
<img alt="Loading..." src="templates/images/loading.gif" />
</div>
</div>
<div class="RSSAggrCont" rssnum="5" rss_url="http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml">
<div class="loading_rss">
<img alt="Loading..." src="templates/images/loading.gif" />
</div>
</div>
<script language="javascript" type="text/javascript">
$(document).ready( function() {
$('div.RSSAggrCont').aRSSFeed();
} );
</script>
Step 2. CSS
Here are used CSS styles.
templates/css/styles.css
.RSSAggrCont {
border:1px solid #AAA;
-moz-box-shadow:0 0 10px #ccc;
-webkit-box-shadow: 0 0 10px #ccc;
width:500px;
padding:10px;
background:#f3f3f3;
margin:15px;
float:left;
}
/* RSS Feeds */
.rss_item_wrapper {
padding-bottom: 8px;
}
.rss_item_wrapper:last-child {
padding-bottom: 0px;
}
.rss_item_header {
font-size:12px;
font-weight:bold;
padding-bottom:0px;
}
.rss_item_header a,
.rss_item_header a:link,
.rss_item_header a:visited,
.rss_item_header a:hover,
.rss_item_header a:active {
font-size:12px;
font-weight:bold;
color:#33c;
}
.rss_item_info {
color:#999;
font-size:9px;
}
.rss_item_desc {
text-align:justify;
font-size: 11px;
}
.rss_read_more {
background-color:#EDEDED;
font-size:11px;
font-weight:normal;
height:30px;
line-height:30px;
vertical-align: middle;
margin-top:2px;
padding:0 9px;
text-align:left;
text-decoration:none;
text-transform:capitalize;
}
.loading_rss {
text-align:center;
width:89px;
height:64px;
background-image:url(../images/loading_bg.png);
z-index: 10;
margin: 10px auto;
}
.loading_rss img {
margin-top: 16px;
}
.loading_rss div {
width:89px;
height:64px;
background-image:url(../images/loading.gif);
background-position:center center;
background-repeat:no-repeat;
}
Step 3. JS
Here are few necessary JS files to our project.
js/jquery.aRSSFeed.js
// jQuery plugin - Simple RSS Aggregator
(function($){
$.fn.aRSSFeed = function() {
return this.each( function(){
var $Cont = $(this);
var iMaxNum = parseInt($Cont.attr( 'rssnum' ) || 0);
var sFeedURL = $Cont.attr('rss_url');
if (sFeedURL == undefined)
return false;
$.getFeed ({
url: 'get_rss_feed.php?url=' + escape(sFeedURL),
success: function(feed) {
if (feed != undefined && feed.items) {
var sCode =
'<div class="rss_feed_wrapper">';
var iCount = 0;
for (var iItemId = 0; iItemId < feed.items.length; iItemId ++) {
var item = feed.items[iItemId];
var sDate;
var a;
var oDate
if (null != (a = item.updated.match(/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)Z/)))
oDate = new Date(a[1], a[2]-1, a[3], a[4], a[5], a[6], 0);
else
oDate = new Date(item.updated);
sDate = oDate.toLocaleString();
sCode +=
'<div class="rss_item_wrapper">' +
'<div class="rss_item_header">' +
'<a href="' + item.link + '" target="_blank">' + item.title + '</a>' +
'</div>' +
'<div class="rss_item_info">' +
'<span><img src="templates/images/clock.png" /> ' + sDate + '</span>' +
'</div>' +
'<div class="rss_item_desc">' + item.description + '</div>' +
'</div>';
iCount ++;
if (iCount == iMaxNum) break;
}
sCode +=
'</div>' +
'<div class="rss_read_more">' +
'<img class="bot_icon_left" src="templates/images/more.png" />' +
'<a href="' + feed.link + '" target="_blank" class="rss_read_more_link">' + feed.title + '</a>' +
'</div>' +
'<div class="clear_both"></div>';
$Cont.html(sCode);
}
}
} );
} );
};
})(jQuery);
js/jquery-1.4.2.min.js and js/jquery.jfeed.js
This is common files – jQuery library with addon. No need to give full code of that file here. It always available as a download package
Step 4. PHP
I hope that most code will easy for your understanding – each function have normal name and commented.
Ok, here are all used PHP files:
index.php
<?php
require_once('templates/rss_page.html');
?>
get_rss_feed.php
<?php $sUrl = (isset($_GET['url']) && $_GET['url'] != '') ? $_GET['url'] : 'http://www.boonex.com/unity/extensions/latest/?rss=1'; header( 'Content-Type: text/xml' ); readfile($sUrl); ?>
Step 5. Images
Also we need several images for our project:
View Live Demo
download in package
Conclusion
Today I told you how to create own RSS aggregator using jQuery library – jFeed. You can use this material to create own scripts into your startups. Good luck!
| I am web developer with huge experience (in web languages and even in system languages). Also I am the founder of current website (and several another). I like to write blogs about web development/design. Feel free to Follow us on Twitter: tweetmeme_screen_name='AramisGC'; | If you Enjoyed our article don't forget to Share the love with your friends. |


how can i control the content display form the feed .. whn i try this with feedburner feed link its shows the full content.. i need post title + short intro about the post..
heyho…well done. thx
I would like to import multiple rss Feeds and ORDERthem by date in one list. Do you have a suggestion where to look? there is alot of stuff, using webservices to combine them. but i want to collect all RSS Feeds, relevant to the students of my course…and i want them to be realtime. so they need to be imported on client-side.
So just follow sample 2, it collect all elements and sorting its by date
I use this script and it’s working great in localhost, but when I host that in some server, the page always stay in loading state. I’ve tried to host in different servers but it stay the same. Are there any settings i’m missing?
thanks
I advice you to track all possible JS errors. As example – install Firebug plugin to your Firefox, and look for errors. Possible your custom page have some errors, or, hosting unable to connect to external source of RSS feed
thanks for the reply
I host your sample code to test the result, and it seems that my hosting cannot connect to external source of RSS. Is there probably any settings on the server we need? I’ve tried changing the permission but the page still do not load.
Hi, try to check server variable ‘allow_url_fopen’, try to set to 1 (ON)
possible reading
http://bugs.php.net/bug.php?id=28497
will help
Hi,
Can I do this using classic ASP instead of PHP ? I want to implement the same functionality for an ASP website ? any thoughts ??
Yes, sure, just repeat what I doing in PHP in your ASP, this is small and easy code.
You will need just return RSS feed text in your custom ASP file (instead my get_rss_feed.php)
Hi, this is really great post, but i have some problem.
I am a beginner in a web development, and the problem is same: When i try the demo, everything is Ok, but downloading the source, it doesn’t work.
Maybe that should insert into localhost, or it is not necessary?
I really need your help, because i want to make similar page in my website.
Thank you
Yes, sure, try this at localhost, this sample should work, just because I made it (when prepared this post) at localhost too
Hi, great post. I’ve been looking at improving the rss feed on my site. Now I just need to adapt it to asp.net.
Hi Admin,
Great post on this RSS feed. I’ve tried everything but can’t seem to get it to show up inside of another .php file. No problem if I post it alone. See examples below:
By itself (working)
http://www.ebaytales.com/feed/
Embedded inside a php file (not working):
http://www.ebaytales.com/searchS.php?type=2&cid=24
Just says “Loading..” I know it’s trying because the footer comes through but the feed doesn’t. Viewed the source code and it looks the same on both pages. Not sure what’s going on.
Note: I’ve already set allow_url_fopen = on in the php.ini. Also I I get a java error in IE.
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)
Timestamp: Thu, 10 Mar 2011 06:39:17 UTC
Message: Object expected
Line: 266
Char: 1
Code: 0
URI: http://www.ebaytales.com/searchS.php?type=2&cid=43
266: $(document).ready( function() {
Hope that helps you find the bug.
Thanks in advance.
Mike
Just make sure that you have only one instance of jquery at page (not several calls).
Beware: This won’t work in browsers with Javascript turned off.
Maybe modify the code a bit, so w/ Javascript turned off, it just shows the images?
2 lep
Of course don`t will work, even jQuery library not will work in case if we will disable JS in browser
If you want to display some image/warning in case when JS disabled – you can just drop this image in layout.
And, remove that image in JS. So if JS enabled – your image will removed when page will loaded.
See how it just tries to load the feed but never does
why?
thanks
2 Terry,
Are demo don`t working for you?
It won`t work in my site. See for yourself: http://infodrumtest.zymichost.com/?p=33 . Please help me figure this out. Firebug said there`s an error somewhere, but I`m not familiar with jQuery and can`t fix it myself. Thanks a ton.
2 Iris
Maybe you faced with problem of second jQuery instance?
try to disable jquery of our sample. Possible you already had jQuery in your project?
It doesn’t work for me either. On none of my browsers, maybe because I updated them all recently and there were bunch of modifications.
It doesn’t replace the gif part with the actual feed. Your demo, however, is working. It is just when I am trying to load it from my website or locally that it does not work
Great RSS feeder, just what we were looking for. Just one question – I am not able to decipher how to edit the image size. Is that possible?
Hi Admin,
Hi, great post
How can i increase the large content in my page?
Hello Mani,
What do you mean? Which large content?
Hello Cassie,
Images is provided by RSS providers. If you will provide bigger images in your RSS – you will have bigger images too
Great post! Thanks for the information!
I was wondering if it would be possible to have multiple sources read out of a single feed…
Hello Billy,
Why not, but in this case you have to create another way to pass multiple source URLs to get_rss_feed.php.
And after, read multiple URLs (readfile) and mix results into single XML file.
Hello Faghan,
I can suggest you to test it in firefox browser. And please – activate Fiebug plugin. It seems that you have some another JS error at this page.