Login

Creating an RSS Reader Application

In the previous two articles we discussed how to read and build an RSS document with PHP. In this article we will create an application that uses the concepts discussed in the other two articles. So let’s go straight to the coding.

The Application

The layout of the application is going to be very simple: A navigation bar on the left and a display area in the center. The display area is were we are going to display all the output of the code. I’ve not used any CSS in this application. I’ve instead opted to use HTML formatting to keep things simple.

As for the actual functioning of the application, we are going to use only one page to execute all the code. This is because it reduces the amount of processing power required to run the application. As a result, it makes program execution faster and reduces coding time.

We are also going to create a few functions that will help us to run the script. The functions are stored in a script called "functions.php" and need to be included in the page. In addition to the functions page, we will also need to include the xmlparser.php page, as this will help fulfill one of the core aims of the application.

When a user clicks on a link, that link will send an action variable to the page. The action variable will then be caught by the switch statement, which will in turn process it and compare its value against the conditions set within the case statements of the switch structure. This comparison, if matched, will activate the appropriate function which will use the display area to output the result. The aim of the application is to:

Read an RSS document

Create an RSS document

The second link called "Create an RSS link" is more complicated to implement than the Read RSS document link. This is because there are two different versions of an RSS document and the application has to decide which path to take based on the selection that the user made. Various functions will display various forms to take input and will create an RSS document based on the input. All new RSS documents that are created by this application will be stored in the same directory where the application resides. With a little bit of code you can change the location in which the file should be stored. In the code section we will explore what each and every function does.

When the "Read an RSS document" link is clicked the application simply calls the functions in the xmlparser.php page and then parses the RSS document. The xml parser was created in the first tutorial of the series. Please refer to it if you need to know how it works.

{mospagebreak title=Code}

The logical way to create an application to automate the building of an RSS document would be to offer the user a choice between creating an Enhanced RSS document or a Simple RSS document, and then to proceed from there.

So let’s create a PHP page with a navigation bar on the left that will contain two links:

Read an RSS document

Create an RSS document

The idea is to use one page on which we do everything, so we are going to create a few functions to display information according to whichever link a user has selected.

When the “Read RSS document” link is pressed, the “showfrm” function is activated; it then displays a form requesting the name of the RSS document to be opened. Once you’ve entered and submitted the document name, a function called “showparsedContents” is called; it then parses the document. Below are the showfrm and showparsedContents() functions:

When the “Create RSS document” link is selected, a function called choicefrm() is activated. This function displays a form that requests a name for the RSS document that you want to create, and also requests that you choose between a simple RSS document or an enhanced one.

Fig 2. A form that request what type of RSS document to create…

Then based on your choice, one of the two forms are displayed. Now, let’s say you chose a simple version. A function called simplefrm() will be activated which will request that you enter the required information. Here’s a screen shot:

Fig 3. Simple RSS Document information form…

When you submit the form, the do_simpfrm() function is called, which processes the form variables and then creates the RSS document. Here’s the function that does all that:

//Create the fileif($fp = fopen($filename,"a+")){//write in the filefwrite($fp,"<?xml version=’1.0’?>rn");fwrite($fp,"<rss version=’2.0′>rn");fwrite($fp,"<channel>rn");fwrite($fp,"<title>".$mtitle."</title>rn");fwrite($fp,"<link>".$mlink."</link>rn");fwrite($fp,"<description>".$mdesc."</description>rn");fwrite($fp,"<item>rn");fwrite($fp,"<title>".$t1."</title>rn");fwrite($fp,"<link>".$l1."</link>");fwrite($fp,"<description>".$txt1."</description>rn");fwrite($fp,"</item>rn");fwrite($fp,"<item>rn");fwrite($fp,"<title>".$t2."</title>rn");fwrite($fp,"<link>".$l2."</link>rn");fwrite($fp,"<description>".$txt2."</description>rn");fwrite($fp,"</item>rn");fwrite($fp,"<item>rn");fwrite($fp,"<title>".$t3."</title>rn");fwrite($fp,"<link>".$l3."</link>rn");fwrite($fp,"<description>".$txt3."</description>rn");fwrite($fp,"</item>rn");fwrite($fp,"</channel>rn");fwrite($fp,"</rss>");fclose($fp);echo "The RSS document <b>" .$filename. "</b> has been created and stored.";}else{echo "The file could not be created due to a system error";}}

And with that a simple RSS document is created.

{mospagebreak title=Creating an enhanced RSS document}

If your choice was to create an Enhanced version, a function called efrm() is called which displays a form that requests particular info from you. Below is a screen shot of the form and after that is shown the actual function code:

Once you’ve submitted this form, the do_efrm function is called. The purpose of this function is to create the RSS document with the information that you supplied in the form. The function is shown below:

function do_efrm($filename,$mtitle,$mlink,$mdesc,$me,$wm,
$cright,$lang,$pubdate,$builddate,$gen,$docs,$imgurl,$imgtitle,
$imglink,$t1,$l1,$txt1,$t2,$l2,$txt2){//Create the fileif($fp = fopen($filename,"a+")){fwrite($fp,"<?xml version=’1.0’?>rn");fwrite($fp,"<rss version=’2.0′ >rn");fwrite($fp,"<channel>rn");fwrite($fp,"<title>".$mtitle."</title>rn");fwrite($fp,"<link>".$mlink."</link>rn");fwrite($fp,"<description>".$mdesc."</description>rn");fwrite($fp,"<managingEditor>".
$me."</mangagingEditor>rn");fwrite($fp,"<webMaster>".$wm."</webMaster>rn");fwrite($fp,"<copyright>".$cright."</copyright>rn");fwrite($fp,"<language>".$lang."</language>rn");fwrite($fp,"<pubdate>".$pubdate."</pubdate>rn");fwrite($fp,"<lastbuilddate>".
$builddate."</lastbuilddate>rn");fwrite($fp,"<generator>".$gen."</generator>rn");fwrite($fp,"<docs>".$docs."</docs>rn");fwrite($fp,"<image>rn");fwrite($fp,"<url>".$imgURL."</url>rn");fwrite($fp,"<title>".$imgtitle."</title>rn");fwrite($fp,"<link>".$imglink."</link>rn");fwrite($fp,"</image>rn");fwrite($fp,"<item>rn");fwrite($fp,"<title>".$t1."</title>rn");fwrite($fp,"<link>".$l1."</link>rn");fwrite($fp,"<description>".$txt1."</description>rn");fwrite($fp,"</item>rn");fwrite($fp,"<item>rn");fwrite($fp,"<title>".$t2."</title>rn");fwrite($fp,"<link>".$l2."</link>rn");fwrite($fp,"<description>".$txt2."</description>rn");fwrite($fp,"</item>rn");fwrite($fp,"</channel>rn");fwrite($fp,"</rss>");fclose($fp);echo "The RSS document <b>" .$filename. "</b> has been created and stored.";}else{echo "The file could not be created due to a system error";}}

There you have it! You can now read and create as many RSS documents as you like!

Conclusion

Although this program fulfills its basic aim of creating RSS files, it nevertheless needs some improvements, especially in the area of checking submitted form data. This is important because you do not want to create files with no information. Also, I’ve not done a lot as far as the application design is concerned, because I only wanted to focus on the programming code as opposed to its design. But the building blocks are there for you to create the design to your taste.