Import RSS Feeds using jFeed (jQuery)

Tutorials

This simple tutorial will show you how to Import rss feeds of any site into your own custom area/block of website. It can be used as news imported from yahoo, bbc etc. As we know, RSS feeds are usually used to trasnfer some news for people. Today I’ll tell you about RSS feeds and show you how to make an own RSS aggregator. In the result, you will be able to display various RSS feeds on your website with nite and modern design.

Here is the sample:

Live Demo

[sociallocker]

download in package

[/sociallocker]


Well, download the source files and let’s start coding !


Step 1. HTML

As usual, we start with the HTML.

This is our main page code (with 2 RSS blocks). Pay heed at RSSAggrCont elements. Param ‘rssnum’ means – how many rss elements will be displayed at the page. ‘rss_url’ – is RSS url. I hope that you will be able to change these params easily.

templates/rss_page.html

01 <link href="templates/css/styles.css" rel="stylesheet" type="text/css" />
02 <script language="javascript" type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
03 <script language="javascript" type="text/javascript" src="js/jquery.jfeed.js"></script>
04 <script language="javascript" type="text/javascript" src="js/jquery.aRSSFeed.js"></script>
05 <div class="RSSAggrCont" rssnum="5" rss_url="http://rss.news.yahoo.com/rss/topstories">
06     <div class="loading_rss">
07         <img alt="Loading..." src="templates/images/loading.gif" />
08     </div>
09 </div>
10 <div class="RSSAggrCont" rssnum="5" rss_url="http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml">
11     <div class="loading_rss">
12         <img alt="Loading..." src="templates/images/loading.gif" />
13     </div>
14 </div>
15 <script language="javascript" type="text/javascript">
16 $(document).ready( function() {
17     $('div.RSSAggrCont').aRSSFeed();
18 } );
19 </script>

Step 2. CSS

Here are CSS styles.

templates/css/styles.css

01 .RSSAggrCont {
02     border:1px solid #AAA;
03     -moz-box-shadow:0 0 10px #ccc;
04     -webkit-box-shadow: 0 0 10px #ccc;
05     width:500px;
06     padding:10px;
07     background:#f3f3f3;
08     margin:15px;
09     float:left;
10 }
11 /* RSS Feeds */
12 .rss_item_wrapper {
13     padding-bottom8px;
14 }
15 .rss_item_wrapper:last-child {
16     padding-bottom0px;
17 }
18 .rss_item_header {
19     font-size:12px;
20     font-weight:bold;
21     padding-bottom:0px;
22 }
23 .rss_item_header a,
24 .rss_item_header a:link,
25 .rss_item_header a:visited,
26 .rss_item_header a:hover,
27 .rss_item_header a:active {
28     font-size:12px;
29     font-weight:bold;
30     color:#33c;
31 }
32 .rss_item_info {
33     color:#999;
34     font-size:9px;
35 }
36 .rss_item_desc {
37     text-align:justify;
38     font-size11px;
39 }
40 .rss_read_more {
41     background-color:#EDEDED;
42     font-size:11px;
43     font-weight:normal;
44     height:30px;
45     line-height:30px;
46     vertical-alignmiddle;
47     margin-top:2px;
48     padding:0 9px;
49     text-align:left;
50     text-decoration:none;
51     text-transform:capitalize;
52 }
53 .loading_rss {
54     text-align:center;
55     width:89px;
56     height:64px;
57     background-image:url(../images/loading_bg.png);
58     z-index10;
59     margin10px auto;
60 }
61 .loading_rss img {
62     margin-top16px;
63 }
64 .loading_rss div {
65     width:89px;
66     height:64px;
67     background-image:url(../images/loading.gif);
68     background-position:center center;
69     background-repeat:no-repeat;
70 }

Step 3. JS

Here are few necessary JS files for our project:

js/jquery.aRSSFeed.js

01 // jQuery plugin - Dolphin RSS Aggregator
02 (function($){
03     $.fn.aRSSFeed = function() {
04         return this.each( function(){
05             var $Cont = $(this);
06             var iMaxNum = parseInt($Cont.attr( 'rssnum' ) || 0);
07             var sFeedURL = $Cont.attr('rss_url');
08             if (sFeedURL == undefined)
09                 return false;
10             $.getFeed ({
11                 url: 'get_rss_feed.php?url=' + escape(sFeedURL),
12                 success: function(feed) {
13                     if (feed != undefined && feed.items) {
14                         var sCode =
15                             '<div class="rss_feed_wrapper">';
16                         var iCount = 0;
17                         for (var iItemId = 0; iItemId < feed.items.length; iItemId ++) {
18                             var item = feed.items[iItemId];
19                             var sDate;
20                             var a;
21                             var oDate
22                             if (null != (a = item.updated.match(/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)Z/)))
23                                 oDate = new Date(a[1], a[2]-1, a[3], a[4], a[5], a[6], 0);
24                             else
25                                 oDate = new Date(item.updated);
26                             sDate = oDate.toLocaleString();
27                             sCode +=
28                                 '<div class="rss_item_wrapper">' +
29                                     '<div class="rss_item_header">' +
30                                         '<a href="' + item.link + '" target="_blank">' + item.title + '</a>' +
31                                     '</div>' +
32                                     '<div class="rss_item_info">' +
33                                         '<span><img src="templates/images/clock.png" /> ' + sDate + '</span>' +
34                                     '</div>' +
35                                     '<div class="rss_item_desc">' + item.description + '</div>' +
36                                 '</div>';
37                             iCount ++;
38                             if (iCount == iMaxNum) break;
39                         }
40                         sCode +=
41                             '</div>' +
42                             '<div class="rss_read_more">' +
43                                 '<img class="bot_icon_left" src="templates/images/more.png" />' +
44                                 '<a href="' + feed.link + '" target="_blank" class="rss_read_more_link">' + feed.title + '</a>' +
45                             '</div>' +
46                             '<div class="clear_both"></div>';
47                         $Cont.html(sCode);
48                     }
49                 }
50             } );
51         } );
52     };
53 })(jQuery);

js/jquery-1.4.2.min.js and js/jquery.jfeed.js

This are common files – jQuery library with jFeed library. It is to no purpose to publish full codes of these files here (they are pretty huge). Both are available in our download package

Step 4. PHP

Finally – PHP sources, I think that everything should be easy to understand:

index.php

1 <?php
2 require_once('templates/rss_page.html');
3 ?>

get_rss_feed.php

1 <?php
2 $sUrl = (isset($_GET['url']) && $_GET['url'] != '') ? $_GET['url'] : 'http://www.boonex.com/unity/extensions/latest/?rss=1';
3 header( 'Content-Type: text/xml' );
4 readfile($sUrl);
5 ?>

Step 5. Images

Several images are required for our project:

    clock icon
    loading icon
    loading background icon
    more icon

Live Demo

Conclusion

Today I’ve described you how to create own RSS aggregator using jQuery library – jFeed. You are welcome to use it in your projects. Good luck!

Rate article