RSS (Really Simple Syndication or Rich Site Summary) is a format based on XML, created to easily share web content, and is particularly suitable for news items. You can use it to display headlines on another site or you can use a program called a “news aggregator” to read the news. These programs allow you to subscribe to various RSS feeds from different news sites. The programs automatically go to the web sites to check for updates, so the stories come to you, without you having to check the sites for new stories.
It is a file format that is incorporated by Internet users in their websites or blogs to allow for 'web syndication', making their content available in a format that can be universally understood by other people.
In essence, RSS is a 'mini database' that contains headlines of your web content, including hyperlinks that enable users to link back to the article of their choice.
RSS websites - that is, websites that contain RSS 'feeds' (articles or postings) - typically have colorful graphics to indicate to users that the specific web content is available through RSS feeds. These graphics are usually depicted by orange rectangles that are usually marked with 'RSS' or 'XML'.
What You Need
Depending on your needs, you can embed RSS feeds to your site in different ways. You can use RSS2Javascript if you prefer javascript for embedding. You can also use RSS2HTML or RSS Feed Reader to embed RSS in HTML format. But in this article we will be using MagpieRSS to embed RSS feeds to your site. It comes handy especially when you don't have access to your apache server(.htaccess) via cPanel or locally.
Embedding RSS Through MagpieRSS
First of all, you should have a copy of MagpieRSS files. If you don't, then you can download it here . After that extract it and upload the folder MagpieRSS to your hosting server. Now open the file or web page where you want to put the RSS feeds. This file will need to have a (.php) extension. Wherever you want the feeds to appear, put this code there.
==========================
<?php
define('MAGPIE_DIR', ' yourfolder /magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');
$rss = fetch_rss( ' http://www.yoursite.com/yourfolder /atom.xml' );
//display latest blog content:
$item = $rss->items[0];
$content = $item['atom_content'];
echo "<p>Latest Blog Entry:<br>$content</p>\n";
?>
You will need to edit the parts in Red to match the directory structure on your web host. You can also edit the echo line to fit your needs, (add a style in there, or just remove the phrase "Latest Blog Entry" for instance.)
Also notice that by changing the 0 in the line:
$item = $rss->items[ 0 ];
you will display an entry other than the most recent. For example, to show the third most recent post, put:
$item = $rss->items[ 2 ];
Creating a List of All The Articles Present on the Feed Link
==========================
<?php
define('MAGPIE_DIR', ' yourfolder /magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');
$rss = fetch_rss( ' http://www.yoursite.com/yourfolder /atom.xml' );
//display links recent blog entries:
echo "<ul><em>Latest blog additions:</em>\n";
foreach ($rss->items as $item) { <-- loop starts here
$href = $item['link'];
$title = $item['title'];
$created = $item['created'];
echo "<li><a href=$href>$title</a> $created</li>\n";
}
echo "</ul>";
?>
You should notice in the above that we are actually getting a little more information about the feeds, and then displaying it to the screen. We are using a loop to go through an array of feeds one by one, getting the link to that feed, the title and the date of publishing . Using an echo command to print to the web page . This gets repeated to make a list, with an opening and closing before and after the loop .
Displaying A Single Post Rather Than Getting All Of It.
==========================
$rss = fetch_rss( ' http://www.yoursite.com/yourfolder /atom.xml' );
to add two extra lines as below:
$num_items = 5 ;$rss = fetch_rss( ' http://www.yoursite.com/yourfolder /atom.xml' );
$items = array_slice($rss->items, 0, $num_items);
The 5 above says you want to get 5 posts from the feed. You also need to make one last change from this line:
foreach ($rss->items as $item) {
to be like this, which is now going through the new, smaller array you sliced from the full array:
foreach ( $items as $item) {
That's it. I hope you have enjoyed our tutorial for today.
Did you like the article? Feel free to comment and subscribe to my feed.
No comments:
Post a Comment