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: 

  1. Read an RSS document
  2. 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:

function showfrm(){
 
echo ‘<form id="form1" name="form1" method="post"
action="’ .$_SERVER['PHP_SELF'].’?action=parse">
 
<table width="100%" border="0" cellspacing="1">
   
<tr>
 
    <td>&nbsp;</td>
 
    <td width="31%">&nbsp;</td>
 
    <td width="48%">&nbsp;</td>
 
  </tr>
 
  <tr>
 
    <td width="21%" valign="top"><span class="style1">RSS
Document Location: </span></td>
 
    <td colspan="2" valign="top"><input name="location"
type="text" id="location" size="80" /></td>
    </tr>
 
  <tr>
 
    <td>&nbsp;</td>
 
    <td>&nbsp;</td>
 
    <td>&nbsp;</td>
 
  </tr>
 
  <tr>
 
    <td>&nbsp;</td>
 
    <td><input type="submit" name="Submit"
value="Submit" /></td>
 
    <td>&nbsp;</td>
  
 </tr>
 
</table>';

  echo ‘</form>';
}

Here’s a example of the outcome of this function:

Fig 1. A form that request RSS document name to parse…

{mospagebreak title=Parsing the RSS document}

The next function just parses the RSS document that is allocated it.

function showparsedContents($thelocation){
  parsefile($thelocation);
}

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:

function do_simpfrm($filename,$mtitle,$mlink,$mdesc,$t1,$l1,
$txt1,$t2,$l2,$txt2,$t3,$l3,$txt3){

  //Create the file
 
if($fp = fopen($filename,"a+")){
   
//write in the file
   
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,"<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:

Fig 4. Enhanced RSS Document information form…

function enhancedfrm(){
 
echo ‘<form id="form1" name="form1" method="post"
action="’ .$_SERVER['PHP_SELF'].’?action=efrm">
  
<table width="82%" border="0" cellspacing="1">
    <tr>
      <td colspan="4" valign="top" bgcolor="#999999"><strong>All
information on this form is required: </strong></td>
    </tr>
    <tr>
      <td valign="top"><strong>Title</strong></td>
      <td><input name="mtitle" type="text" size="40" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td valign="top"><strong>Link</strong></td>
      <td><input name="mlink" type="text" size="40" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td valign="top"><strong>Description</strong></td>
      <td><textarea name="mdesc" cols="40"
rows="5"></textarea></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="18%" bgcolor="#999999">&nbsp;</td>
      <td width="33%" bgcolor="#999999">&nbsp;</td>
      <td width="11%" bgcolor="#999999">&nbsp;</td>
      <td width="38%" bgcolor="#999999">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top"><strong>Managing Editor: </strong></td>
      <td valign="top"><input name="me" type="text" id="me"
size="40" /></td>
      <td valign="top"><strong>URL:</strong></td>
      <td valign="top"><input name="imgURL" type="text"
id="imgURL" size="40" /></td>
    </tr>
    <tr>
      <td valign="top"><strong>Web Master: </strong></td>
      <td valign="top"><input name="wm" type="text" id="wm"
size="40" /></td>
      <td valign="top"><strong>Title:</strong></td>
      <td valign="top"><input name="imgTitle" type="text"
id="imgTitle" size="40" /></td>
    </tr>
    <tr>
      <td valign="top"><strong>Publication Date: </strong></td>
      <td valign="top"><input name="pubdate" type="text"
id="pubdate" size="40" /></td>
      <td valign="top"><strong>Link:</strong></td>
      <td valign="top"><input name="imgLink" type="text"
id="imgLink" size="40" /></td>
    </tr>
    <tr>
      <td valign="top"><strong>Last Build Date: </strong></td>
      <td valign="top"><input name="bdate" type="text" id="bdate"
size="40" /></td>
      <td valign="top"><strong>Width:</strong></td>
      <td valign="top"><input name="width" type="text" id="width"
size="30" /></td>
    </tr>
    <tr>
      <td valign="top"><strong>Generator:</strong></td>
      <td valign="top"><input name="gen" type="text" id="gen"
size="40" /></td>
      <td valign="top"><strong>Height:</strong></td>
      <td valign="top"><input name="height" type="text"
id="height" size="30" /></td>
    </tr>
    <tr>
      <td valign="top"><strong>Docs:</strong></td>
      <td valign="top"><input name="docs" type="text" id="docs"
size="40" /></td>
      <td valign="top">&nbsp;</td>
      <td valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top"><strong>CopyRight Notice: </strong></td>
      <td valign="top"><input name="cright" type="text"
id="cright" size="40" /></td>
      <td valign="top">&nbsp;</td>
      <td valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top"><strong>Language:</strong></td>
      <td valign="top"><input name="lang" type="text" id="lang"
size="40" /></td>
      <td valign="top">&nbsp;</td>
      <td valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td bgcolor="#666666">&nbsp;</td>
      <td bgcolor="#666666">&nbsp;</td>
      <td bgcolor="#666666">&nbsp;</td>
      <td bgcolor="#666666">&nbsp;</td>
    </tr>
    <tr>
      <td><strong>Title:</strong></td>
      <td><input name="t1" type="text" id="t1" size="40" /></td>
      <td><strong>Title:</strong></td>
      <td><input name="t2" type="text" id="t2" size="40" /></td>
    </tr>
    <tr>
      <td><strong>Link:</strong></td>
      <td><input name="l1" type="text" id="l1" size="40" /></td>
      <td><strong>Link:</strong></td>
      <td><input name="l2" type="text" id="l2" size="40" /></td>
    </tr>
    <tr>
      <td valign="top"><strong>Description:</strong></td>
      <td><textarea name="d1" cols="40" rows="5"
id="d1"></textarea></td>
      <td valign="top"><strong>Description:</strong></td>
      <td valign="top"><textarea name="d2" cols="40" rows="5"
id="d2"></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>';
echo ‘</form>';
}

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 file
 
if($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.

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort