Go Back   Wiki NewForum | Latest Entertainment News > General Discussion


Quick and Easy Site Content with RSS and PHP


Reply
Views: 1828  
Thread Tools Rate Thread
  #1  
Old 12-15-2009, 06:36 AM
bholus10 bholus10 is offline
Award Winner
 
Join Date: Nov 2009
Posts: 10,043
Default Quick and Easy Site Content with RSS and PHP

One of the many benefits of Really Simple Syndication or RSS is the promise of a constant stream of fresh and updated content to your website. As we all know, having new

content on a regular basis delivers a Search Engine Optimization benefit because spiders seem to love new content. Of course if the RSS feed is chosen carefully, the fresh content can also be a great service to your website's readers.

Unfortunately, this promise of fresh content is often unfulfilled because dealing with the technology required adding this content to your site can seem confusing. Although the steps are actually pretty simple, getting content from an RSS feed added to your site can be daunting.

Some easy ways of adding an RSS feed to your site would be to use one of several free JavaScripts that are available on the web. Simply paste a few lines of JavaScript into your page and you are done. If all you care about is providing fresh content to your sites readers, this may be a good enough solution. One advantage of this solution is that since you are only adding scripting, you will not be linking to other websites in a way that a web spider will be able to see.

The fact that a web spider cannot see the updated content is also a liability of the JavaScript solution. We would prefer that GoogleBot and MSN recognize that we have new

content on our sites and reward us for our efforts. If we are going to accomplish this goal, then we are going to have to come up with a solution that can turn the raw RSS/XML from the external feed into actual markup in our pages.

Doing this will require some programming ability on our part. We need a programming language that is quick, easy to learn, and is preferably not too much different from the standard HTML that we are used to. PHP is just about perfect for the job. PHP will allow us to write a short, simple program to fetch an RSS feed from a website, store a copy of that RSS feed locally for our own use, parse the appropriate data out of that feed, and then format a site bit with the HTML of our own choosing. (If that sounds difficult, don't worry, I'll step you through the process below)

How we will do this is ultimately going to be as easy for you as learning a new HTML tag, pasting it into your web page, and uploading a couple of files to your server. I am going to brush over the basics, but give you an exact step by step method to

get RSS files fully parsed into HTML and added to your pages. I'm going to assume that you've already got a page ready to add your RSS feed to, that this page is a PHP or PHTML page, and you've already got your perfect RSS feed picked out.

For this demo I will use the RSS feed from Site-Reference ( Example 1 ). We will take the data from that feed, and transform it into some HTML we can use, and then take that HTML and place it on our sites and webpages.

The first thing that you will do is open a HTML or PHP editor. Windows notepad or WordPad will do. Now copy the code from the code sample ( Example 2 ) into your editor and

save the file as SimpleReader.php. You can also save it as SimpleReader.txt and rename it later. Take a minute or so to look at what’s in this file, some of it may seem confusing but chances are some of it looks familiar too.

Looks a lot like HTML doesn't it ? Before you upload it, you may want to take a second to replace the default RSS URLs, with the URLs to other RSS feeds of your choosing. The part you should change looks like this:


// Here are the feeds - you can add to them or change them
$RSSFEEDS = array(
0 => "http://www.site-reference.com/xml.php?c=all",
1 => "http://rss.cnn.com/rss/cnn_topstories.rss",
2 => "http://rss.slashdot.org/Slashdot/slashdot",
);


To change the RSS feed that displays on your site, just change the first URL to the feed of your choice.

Upload this file to a location on your web server, and point your web browser at the file. The PHP code in the file will execute on your web server and send an HTML page down to your browser that has the RSS converted into a webpage. If we've done everything correctly what we should see is ( this ). For the novice who is reading, allow me to point out that the PHP in the file is executing on the web server, not in your browser like a html page. The output from the PHP script that runs on the server, is the page that you see in your browser. And it is this page that we are going to insert into your perfect page that’s awaiting its new RSS feed!

At this point we've got the RSS feed being converted to HTML, now we can take a few minutes to do a little bit of customizing if we want to and figure out how to get this HTML into our web page. Fortunately that’s just as easy!

Before you jump in and begin customizing, let’s take a moment to understand that what we are customizing here is not an entire page. We are customizing just a tiny "piece" of a page that is going to find a home in your own page.

Take a look at the file you saved, and notice that near the top there is some code that looks like this:

//
// Makes a pretty HTML page bit from the title,
// description and link
//
function FormatRow($title, $description, $link) {
return <<

$title


$description


Read more...






HTML;
}

To customize the format of the Feed Entries all we need to do is edit the HTML you see between the comments. Feel free to change anything you like and get as creative as you want. You can add any HTML you need such as Header tags, divs, spans, and

tables. You probably guessed if you didn't already know - $title, $description, and $link are the title description and link from the RSS feed. How you customize the HTML will depend where you plan to place the feed and the bigger picture of your entire page.

Reply With Quote
Reply

Latest News in General Discussion





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.