Recent Topics

RSS Feeder

Started by on Mar 12, 2005 – Contents updated: Mar 12, 2005

Mar 12, 2005 10:15    

Can anyone recommend a feeder that will display RSS feeds in HTML form on an HTML web page, not a PHP one?

Mar 12, 2005 10:51

thats a tough one, and here is why ...

typically the feeds are actually parsed out of an external file, thats been written to by the feed reader OR theyre inserted like they would be if they were piped to a temp file first. Either way, you need something to do the work. and plain ole' HTML cannot do it.

If you are deadset against <?php ... are you able to use, and open to using a server side include (CGI include)? if so, and if you have access to creating an .htaccess i can show you how to make cgi callouts, like SSI's available within .html files. From there you just need the feed reader, at that point the next question is are you able to have the feed reader itself be php based?

Mar 12, 2005 11:45

Why do you especially want html output? - is it portability or search engine friendly pages or something else - because as whoo points out it's not an easy matter in itself.

Mar 12, 2005 11:53

nope, I think you misunderstand me.

the output isnt the hitch -- its the fact that he/she wants to run scripts, be they perl/CGI/PHP using a plain ole' HTML file, HTML is not a scripting language :)

Server side includes are the way to go in this case, fix up an .htaccess that allows html files to be be server parsed, like so:


AddType text/x-server-parsed-html .html

or


AddHandler server-parsed .html

and You just turned a plain ole HTML file into something that can use cgi.

After that, use nearly any feedreader that writes to an external file, there are more than a few of them. I can provide links if needed.

then call the feed using a simple include within the .html file ...


<!--#include virtual="/myfeed/feed.txt" -->

voila done :)

Mar 13, 2005 17:12

Yes, I want HTML output for search engine friendly pages. Unfortunately, currently the page I am going to be displaying the feeds on is an shtml page, and can't be modified to php until the entire script is written for php.

Mar 13, 2005 18:27

The page I am using is running SSI; at least I know it was, but I have just got a new computer, and I am having problems getting some scripts to display, I think usually ones that employ JavaScript somewhere.

Mar 14, 2005 01:49

.shtml pages are server parsed and using SSI includes you can insert RSS feeds... no need to worry about switching that over to php unless you want to for another reason..

ill dig around in my files and give you the link to the old RSS feeded I used to use -- it was fairly easy to set up, and ...

well here is the original script : http://www.designplace.org/scripts.php?page=1&c_id=14

I managed to locate a souped up version of that somewhere else, but that version should do you just fine. Set up properly, Its going to create a page with the feed outputs, then you just include that page into your .shtml page using the include stuff I showed you above.

Mar 16, 2005 00:49

yes, that would be it :) it's a great script, worked for me flawlessly -- hope it helps you :)

Mar 16, 2005 18:02

Any idea how I would modify the feed to display the date of posting as well as the titles of each post?

Mar 16, 2005 20:59

i would have to re-install it and take a look myself .. i'm not using it currently so off the top of my head no ... but i can point to you to PHP's date function: http://www.php.net/date

And al of that depends on whether or not you want the date it's put on your site or the date you snagged it from the other site, or the date it was first posted to the original site. Just those minor details :)

Mar 16, 2005 21:44

Here is the code for the two files:
index.php


<?PHP

  // OK this file will get the headlines from Slashdot... 
  include("rdf.php"); // Include out RDF util class

?>
<html>

<head>
<title>RSS News Feed Example</title>
<style>
<!--
td            { font-family: Verdana; font-size: 8pt }
td.title      { font-family: Verdana; font-size: 10pt }
A:link        { background-color: transparent; color: #0000ff; text-decoration: none; }
A:visited     { background-color: transparent; color: #0000ff; text-decoration: none; }
A:hover       { background-color: transparent; color: #ff0000; text-decoration: none; }
A:active      { background-color: transparent; color: #ff0000; text-decoration: none; }
-->
</style>
</head>
<body>

<?php
  // This will grab the news feed, start at item 0 (the first item) and pull 10 items.  It is refreshed every 6hrs.
  echo getfeed('http://blogger.directorygold.com/blogs/xmlsrv/rss2.php?blog=2', '0', '10', '360');
?>

</body>

</html>

rdf.php


<?PHP

// Based upon the script located at the URL..
// http://www.designplace.org/scripts.php?page=1&c_id=14

// Modified by Axe (http://www.reptilerooms.com) on October 2nd, 2003.
// [Changes]
//    - All functions now "return" a value instead of echoing it directly.
//    - basename($url) changed to md5($url) to prevent file overwriting
//      when two sites share an identical filename for their RSS feeds.
//    - Added the $dirname variable so you can store the RSS feeds in a
//      separate directory and they're not cluttering up your main script
//      directory (and so you don't have to chmod 777 your main script
//      directory.
//    - Modified the output a little bit to send back a table.
//    - Added $sepcolor variable for line separator between headlines
//    - Added the getfeed() function so that we didn't have to have half a
//      dozen lines in the main script to call the feed.
//    - Added $useremail variable.  Set this to a contact E-Mail address.  This
//      is sent out as part of the User-Agent, so that people you are grabbing
//      feeds from know how to contact you if they so desire.

// Config some variables..
$dirname = 'rss/';  // This is the directory where the news feed files are stored - Remember to CHMOD 777 this directory.
$sepcolor = '#000000';
$useremail = 'webmaster@directorygold.com';

// This is the function to get the feed and send back the table.

// getfeed(
//   url of feed,
//   first link to display,
//   total number of links to display,
//   update interval,
//   random news or in order?
// );

function getfeed($url, $startlink = 0, $numlinks = 5, $interval = 60, $randomise = False) {
  $rdf = new rdf(); // Create a new rdf class
  $base = md5($url); // Work out the name that we are going to use as a local cached file.
  $rdf->getRDF($url, $interval); // Get the RDF Headlines...
  $sendback = $rdf->displayRDF($base, $randomise, $startlink, $numlinks); // Display the RDF headlines...
  return $sendback;
}

class rdf // A util Class for reading in RDF XML Headline style files...
  { 
  function createRDF($url, $timeToLive)
  {
    global $dirname;
    $basename = $dirname.md5($url);
    if (!file_exists($basename))
    {
      touch($basename, ($timeToLive+1)); // Make it older then the time to live...
//      The line below has been commented out to prevent other system users from modifying the news.
//      The web server is the owner of the file, and it has write permission by default.
//      chmod($basename, 0777) ;
    }
  }
  
  function getRDF($url, $timeToLive) // This function will read the data in from the passed in URL if it is required.
  {
    global $dirname, $useremail;
    $timeToLive *= 60; // convert timeToLive into secs
    $basename = $dirname.md5($url) ; // Get the basename of URL for the cached file name. We do this here, cos we need to do it more than once
  
    $this->createRDF($url, $timeToLive);
  
    $timestamp = filemtime($basename); // Get the timestamp of the file.
    $age = (time() - $timestamp); // Work out how old the file is that we have already..
  
    if($age > $timeToLive) // If the file is too old, then we need to refresh it from the URL
    {

      $host = ereg_replace(".*http://","",$url); // Remove the leading http://
      $host = ereg_replace("/.*","",$host); // Remove everything after the first /
      $rdfHandle = fsockopen ($host, 80, $errno, $errstr, 30);
      $filepath = ereg_replace(".*http://","",$url); // Remove the leading http://
      $filepath = ereg_replace($host, "", $filepath); // Now remove the hostname from what's left and we've got the path & filename.
      if (!$rdfHandle) {
        $rdfData .= "<title>$errstr ($errno)</title><br>\n";
      } else {
        fputs ($rdfHandle, "GET $filepath HTTP/1.0\r\nHost: $host\r\n");
        fputs ($rdfHandle, "User-Agent: RSS News Feed Reader v/1.0 ($useremail)\r\n\r\n");
        while (!feof($rdfHandle)) {
          $rdfData .= fgets ($rdfHandle,128);
        }
      fclose ($rdfHandle);
      }

      // OK there is more recent news, so rewrite the cached news file..
      $localFile = fopen($basename, "w") ; // Open the local file for writing
      fwrite($localFile, $rdfData ) ; // Pump in all the data into the file.
      fclose($localFile) ; // Close the local file after writing to it
    } // end IF
  } // end getRDF
  
  function formatLink($item) // Removes spurious tags from the link that we don't want
  {
    $link = ereg_replace(".*<link>","",$item); // Remove the leading <link> tags
    $link = ereg_replace("</link>.*","",$link); // Remove the closing </link> tags
    $title = ereg_replace(".*<title>","",$item); // Remove the leading <title> tags
    $title = ereg_replace("</title>.*","",$title); // Remove the closing </title> tags


/*
    // Convert all the &amp; symbols...
    $in = array ("'&(amp|#38);'i");
    $out = array ("&");
    $title = preg_replace($in, $out, $title); 
*/

    // Convert all the other codes in there.
    $in = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                 "'<[\/\!]*?[^<>]*?>'si",           // Strip out html tags
                 "'([\r\n])[\s]+'",                 // Strip out white space
                 "'&(quot|#34);'i",                 // Replace html entities
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(\d+);'e");                    // evaluate as php

    $out = array ("",
                  "",
                  "\\1",
                  "\"",
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(\\1)");


    $title = preg_replace($in, $out, $title); 


    if ($title) // If we got anything left after all that trimming...
    // Choose how you want the link formatted here... This has no underline, and opens in a new window...
    $sendback .= "<tr><td valign=\"top\">&nbsp;<font face=\"Wingdings\" size=\"1\">l</font>&nbsp;<td valign=\"top\"><a href=\"$link\" target=\"_blank\">$title</a><br>$descrip</td></tr>\n";
    return $sendback;
  } // end formatLink
  
  function displayRDF($rdf, $randomise = True, $startlink=0, $numLinks = 5)
  {
    global $dirname, $sepcolor;

    // This line opens up the table. 
    $sendback = '<table border="0" cellpadding="0" cellspacing="0">';

    $rdf = $dirname.$rdf;
    $localFile = fopen($rdf, "r"); // OK open up the local rdf file for reading
    clearstatcache() ; // Clear out the file size cache
    $rdfData = fread($localFile, filesize($rdf)); // Read in the data to memory
    fclose($localFile); // Close down the open file.
  
    // Get rid of all spurious leading and closing rdf data from the data in memory
    $rdfData = ereg_replace("<\?xml.*/image>","",$rdfData);
    $rdfData = ereg_replace("</rdf.*","",$rdfData);
    $rdfData = ereg_replace("[\r,\n]", "", $rdfData);
    $rdfData = chop($rdfData); // Strip any whitespace from the end of the string
  
    $rdfArray = explode("</item>",$rdfData); // Split up the string into an array to make it more manageable
    $max = sizeof($rdfArray); // See how many items we have got
    if ($max > 1)
    {

      if ($randomise) // We need to do different stuff if we are to randomise the links... 
      {
        // The links will be randomised so we want a different message to the user....
        // The max -1 is to compensate for the 0 indexed array structure..
        $links = array_rand( $rdfArray, $numLinks ) ; // OK select the keys of the links at random from the array..
        $upperLimit = $numLinks ; // Set this to the number of links to be displayed
      } else {
        $links = array_keys($rdfArray) ; // Give the keys to be displayed all the links we have parsed..
        $upperLimit = $numLinks ; // Set the upper Limit to be all of the headlines
      }
  
      // Display the links...
      for ($i = $startlink ; $i < ($startlink + $upperLimit) ; $i++ ) {
        if ($i != $startlink) $sendback .= "<tr height=\"1\" ><td></td><td width=\"100%\" bgcolor=\"$sepcolor\"></td></tr>\n";
        $sendback .= $this->formatLink($rdfArray[$links[$i]]);
      }

    // Yeah, we didn't find any links ;)
    } else {
      $sendback .= "<tr><td colspan=\"2\">Sorry, no links found in the RDF file $base from $url...</td></tr>" ;
    }

    // This line closes the table. 
    $sendback .= "</table>";
    return $sendback;
  } 
}

?>

My knowledge of PHP is almost nonexistent, so I don't even know which file I would need to modify. The date I would be after is the date the post was originally posted to the original site.

Mar 16, 2005 22:01

well, first question then is that date available somewhere? if so where?

Mar 16, 2005 22:21

Initially, I would be using a b2e blog. The date is published in each post, so I'm sure it's available somewhere.


Form is loading...

CMS software – This forum is powered by b2evolution CMS, a complete engine for your website.