LastRSS is a free lightweight PHP RSS Parser that supports server caching. I use it on this site to
produce the 'News' and 'Code Tips' in the footer of this site. I also use it in the actual 'Code Tips' page
except there I've reconfigured the limiter to produce the amount to show all the code tips. In any case, the
following code tip will help you to use LastRSS as I've done.
Note: This tutorial isn't designed to tell you all the
workings of LastRSS only how to use it, so if your wanting to find out how the class works your either going to
have to go through the code by yourself or find a site tutorial that explains it.
You can download necessary files for this code tip at
http://lastrss.oslab.net.
OR download the zip file straight from
here!
AND take a look at the source code
here!
The file is only KB's and will have marginal impact your page loading time. when it's downloaded just unzip the file
into your includes folder which ever that might be. If you've no idea what I'm talking about I'll assume your just
starting out with PHP. The rest of you can
continue the tutorial here.
PHP supports Server Side Includes (SSI) which makes it extra easy to put together a site and keep it updated quickly
and efficiently. For instance, take my navigation menu on the left. Do you actually think I would update that for
every page on this site? Of course not, it's simpler for me to create a free PHP account for my web site and write the code
in every page that slots in the navigation sub-section automatically which consists of all of one line.
<?php include 'navigation.php' ?>
You liked that didn't you? but remember you must set the location of your includes folder either by altering the php.ini
file to a folder of your choosing, or set the includes folder relatively by the default position which is either
'yourHost/www' or if you use XAMPP as personal development platform it will probably be 'yourFolderDirectory/XAMPP/htdocs'.
Another alternative to this however is to set the server variable in an .htaccess file which is my preference.
Inside the .htaccess file just type on a new line:
For Windows:
php_value include_path ".;C:/Absolute/Directory/To/Your/Includes/Folder"
For *nix:
php_value include_path ".:Absolute/Directory/To/Your/Includes/Folder"
Apparently, the '.;' or '.:' is super important as PHP interprets this as go to the highest level directory.
If you want more information on .htaccess files then I recommend this
UserTools tutorial.
It's a pretty good list of all the things you can do via .htaccess which is considerable for the few bytes and minimal
programming it needs to set it up.
So by now you've downloaded the 'LastRSS.php' file and added it to your includes folder and have
included it in the page you wish to use it on. What next? The good part...
You could extend the 'LastRSS' class include if you want but I recommend creating a new include calling it
'WhatEverYouLike.php'. I prefer to separate the 'LastRSS' code from my own code to make it easier for me to understand
where everything is and to reuse the code later on. In the new include we've just created we're going to create a function
that looks like this;
1 <?php
2 function showRSS($url)
3 {
4 global $rss;
5 if($rs = $rss->get($url))
6 {
7 foreach($rs['items'] as $item)
8 {
9 echo '<p><b><A href="'.$item['link'].'">'.$item['title'].'</a></b> - '.$item['pubDate'].'<br />
10 '.$item['description'].'<p>';
11 }
12
13 if ($rs['items_count'] <= 0)
14 {
15 echo '<p>RSS coming soon!</p>';
16 }
17 }
18 }
19 ?>
At line 4, this simple function uses $rss class as a global, then using its 'get($url)'' function at line 5 it then proceeds to
read the RSS file. Where is the RSS file? We'll get to that later. At line 7 it begins a loop that goes through all the items
found in the RSS file and display them during lines 9 and 10. Remember your single quote marks (') followed by your periods (.)
so PHP knows your cutting from display syntax to server syntax and your wanting the server to see this. Reverse the process to
period - single quote mark to cut back in to display syntax.
The following code "$item['description']" refers to the items found in your RSS file. Description refers to the RSS tag
<description>. To understand this better,
look at my RSS file and check the page source.
Identify the <description> tag and it's relationship to all the other tags. If it helps copy the source to notepad and
use it as a template. Just remember to save it as a .rss file. It's pretty generic so you shouldn't have any problems using it.
Lines 13 to 16 are your full back if no RSS items are found. It simply counts the items in the RSS document and displays a message
if none are found.
So thats the function we'll use to display RSS items on our page, but we need to deliver the function the url parameter. So lets do
that now;
1 <?php
2 $rss = new LastRSS;
3 $rss->cache_dir = './cache';
4 $rss->cache_time = 14400;
5 $rss->cp = 'UTF-8';
6 $rss->items_limit = 5;
7
8 $rss_newsAddress = './RSS/news.rss';
9 ?>
So line 2 instanciates $rss as a new LastRSS object. At line 3 we declare a cache folder. In here PHP will create
cache files so that after every cache interval declared in line 4 it creates a new record of items found in the RSS
file. You have to create the folder manually in your folder structure, you don't have to call it cache, but it makes
sense if you do. the cache_time variable counts in seconds so a value of 3600 counts as an hour. I recommend a cache
time of a minimum of 3 - 4 hours. Any shorter, and I think your server host might have something to say about it. Line
5 simply states the unicode transaction format you wish to use. UTF-8 is a popular choice so I stick with that. You can
guess what line 6 states. This is the Items limiter and by setting its value your effectively saying I only want to see
'x' amount of items. Line 8 is a custom variable not related to the $rss object we've created. It's simply the address
to the RSS file we want to read. Point it at your rss file, or somebody elses, it doesn't matter as long as it uses a
internet protocol. No absolute address' starting from your c:/ drive or where ever. It won't work.
So with our $rss object created all that remains is to start up the showRSS() function we created earlier. Add this to your
php document you wish to view the RSS feed on.
1 <?php
2 ShowRSS($rss_newsAddress);
3 ?>
And there you have it. If everything is setup properly, and you have all your file directories setup correctly, which
sometimes can be trickier then it sounds if you use a lot of SSI's, then you should be seeing your RSS items.
Any problems, feel free to
contact me and i'll edit this document to reflect your comments or problems'