Here's what I want to do.

  • northjersey78
  • Graduate
  • Graduate
  • User avatar
  • Posts: 173
  • Loc: New Jersey

Post 3+ Months Ago

I own http://www.nysportsboard.com - I started this just as a phpbb forum, but have decided to incorporate more info ibnto the main page and use the forum as an add-on. So I moved everything for the forum to http://www.nysportsboard.com/forum and now have a blank html page at the root http://www.nysportsboard.com.

Not sure what exactly I will have on there, but for now - I wanted to have a running rss feed of any stories related to NY Sports (Yankees, Mets, Giants, etc...) - I can use a service like Feed Blendr to mash several feeds to 1, or list them seperately.

What would be the best way to display the feed(s) on my site? I did a few Google searches but found either extremely limited, cheesy looking solutions. Is there a simple code I can write to accomplish this?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

This is about as "simple code" as I can think of to eat RSS.
Requires PHP 5

Using http://www.nytimes.com/services/xml/rss/nyt/Sports.xml as an example.

build.php
This can be run via cron/automated-task.
It will,
1) Make a backup of the current page/index,
2) Fetch a single ("mashed") feed,
3) Load an index/page template & replace an empty marker element with HTML-ified RSS items.
4) Publish a new, static index/page file.
5) Log what happened to "rss.log", or output to the browser window if "?human=1" is used to access the file.
PHP Code: [ Select ]
<?php
 
// LOGGING FUNCTION
 
   // Access with "filename.php?human=1" to output status message to browser.
 
   // Absense of "human" causes status messages to be logged to file "rss.log"
 
   function notify($message){
 
      if(!empty($_GET['human'])){die($message);}
 
      else{file_put_contents('rss.log', '[' . date('r') . "] $message\r\n", FILE_APPEND);exit;}
 
   }
 
// CONFIG
 
   // File Extension to publish with (No Period/Dot)
 
   $ext = 'html';
 
   // URL of RSS feed
 
   $rssURL = 'http://www.nytimes.com/services/xml/rss/nyt/Sports.xml';
 
// BACKUP
 
   // Backup current static page
 
   if(file_exists("index.$ext")){
 
      if(!copy("index.$ext", "index.$ext.bak")){
 
         notify("Aborted: Backup couldn't be made.");
 
      }
 
   }
 
// FETCH
 
   // Get RSS feed
 
   if(!$rss = file_get_contents($rssURL)){
 
      notify("Aborted: Couldn't fetch $rssURL");
 
   }
 
   // Find position of first Item
 
   if(!$beginning = strpos($rss, '<item>')){
 
      notify("Aborted: No items found");
 
   }
 
   // Find position of last Item
 
   if(!$end = strrpos($rss, '</item>') + 7){
 
      notify("Aborted: Malformed RSS $rssURL");
 
   }
 
   // Find length of all items
 
   $len = strlen($rss) - $beginning - (strlen($rss) - $end);
 
   // Pull Items into new string
 
   $items = substr($rss, $beginning, $len);
 
// REPLACEMENT
 
   // Elements to match
 
   $rssElms = Array(
 
      '<item>', '</item>', // div
 
      '<title>', '</title>', // h3
 
      '<description>', '</description>', // p
 
      '<link>', '</link>', // a
 
      '<author>', '</author>', // b~br
 
      '<pubdate>', '</pubdate>', // b~br
 
      '<guid isPermaLink="false">', '</guid>', // a class="off"
 
      '<guid isPermaLink="true">', // a class="permalink"
 
      '<guid>' // a class="permalink"
 
   );
 
   // Elements to replace matches with
 
   $htmlElms = Array(
 
      '<div>', '</div>', // item
 
      '<h3>', '</h3>', // title
 
      '<p>', '</p>', // description
 
      '<a href="', '">Full Story</a>', // link
 
      '<b>Source: </b>', '<br/>', // author
 
      '<b>Published: </b>', '<br/>', // pubdate
 
      '<a class="off" href="', '">Permanent Link</a>', // guid isPermaLink="false"
 
      '<a class="permalink" href="', // guid isPermaLink="true"
 
      '<a class="permalink" href="' // guid
 
   );
 
   // Do replacements
 
   $html = str_ireplace($rssElms, $htmlElms, $items);
 
// TEMPLATE
 
   // Load index template
 
   if(!$index = file_get_contents('index.template.xml')){
 
      notify("Aborted: Couldn't load template");
 
   }
 
   // Replace the empty DIV from the template with the new HTML
 
   $index = str_replace('<div id="headlines"/>', ('<div id="headlines">' . $html . '</div>'), $index);
 
// PUBLISH
 
   if(!file_put_contents("index.$ext", $index, LOCK_EX)){
 
      notify("Aborted: New index.$ext couldn't be written");
 
   }
 
// FINISHED
 
   echo notify("Published: New index.$ext written successfully from $rssURL");
 
?>
  1. <?php
  2.  
  3. // LOGGING FUNCTION
  4.  
  5.    // Access with "filename.php?human=1" to output status message to browser.
  6.  
  7.    // Absense of "human" causes status messages to be logged to file "rss.log"
  8.  
  9.    function notify($message){
  10.  
  11.       if(!empty($_GET['human'])){die($message);}
  12.  
  13.       else{file_put_contents('rss.log', '[' . date('r') . "] $message\r\n", FILE_APPEND);exit;}
  14.  
  15.    }
  16.  
  17. // CONFIG
  18.  
  19.    // File Extension to publish with (No Period/Dot)
  20.  
  21.    $ext = 'html';
  22.  
  23.    // URL of RSS feed
  24.  
  25.    $rssURL = 'http://www.nytimes.com/services/xml/rss/nyt/Sports.xml';
  26.  
  27. // BACKUP
  28.  
  29.    // Backup current static page
  30.  
  31.    if(file_exists("index.$ext")){
  32.  
  33.       if(!copy("index.$ext", "index.$ext.bak")){
  34.  
  35.          notify("Aborted: Backup couldn't be made.");
  36.  
  37.       }
  38.  
  39.    }
  40.  
  41. // FETCH
  42.  
  43.    // Get RSS feed
  44.  
  45.    if(!$rss = file_get_contents($rssURL)){
  46.  
  47.       notify("Aborted: Couldn't fetch $rssURL");
  48.  
  49.    }
  50.  
  51.    // Find position of first Item
  52.  
  53.    if(!$beginning = strpos($rss, '<item>')){
  54.  
  55.       notify("Aborted: No items found");
  56.  
  57.    }
  58.  
  59.    // Find position of last Item
  60.  
  61.    if(!$end = strrpos($rss, '</item>') + 7){
  62.  
  63.       notify("Aborted: Malformed RSS $rssURL");
  64.  
  65.    }
  66.  
  67.    // Find length of all items
  68.  
  69.    $len = strlen($rss) - $beginning - (strlen($rss) - $end);
  70.  
  71.    // Pull Items into new string
  72.  
  73.    $items = substr($rss, $beginning, $len);
  74.  
  75. // REPLACEMENT
  76.  
  77.    // Elements to match
  78.  
  79.    $rssElms = Array(
  80.  
  81.       '<item>', '</item>', // div
  82.  
  83.       '<title>', '</title>', // h3
  84.  
  85.       '<description>', '</description>', // p
  86.  
  87.       '<link>', '</link>', // a
  88.  
  89.       '<author>', '</author>', // b~br
  90.  
  91.       '<pubdate>', '</pubdate>', // b~br
  92.  
  93.       '<guid isPermaLink="false">', '</guid>', // a class="off"
  94.  
  95.       '<guid isPermaLink="true">', // a class="permalink"
  96.  
  97.       '<guid>' // a class="permalink"
  98.  
  99.    );
  100.  
  101.    // Elements to replace matches with
  102.  
  103.    $htmlElms = Array(
  104.  
  105.       '<div>', '</div>', // item
  106.  
  107.       '<h3>', '</h3>', // title
  108.  
  109.       '<p>', '</p>', // description
  110.  
  111.       '<a href="', '">Full Story</a>', // link
  112.  
  113.       '<b>Source: </b>', '<br/>', // author
  114.  
  115.       '<b>Published: </b>', '<br/>', // pubdate
  116.  
  117.       '<a class="off" href="', '">Permanent Link</a>', // guid isPermaLink="false"
  118.  
  119.       '<a class="permalink" href="', // guid isPermaLink="true"
  120.  
  121.       '<a class="permalink" href="' // guid
  122.  
  123.    );
  124.  
  125.    // Do replacements
  126.  
  127.    $html = str_ireplace($rssElms, $htmlElms, $items);
  128.  
  129. // TEMPLATE
  130.  
  131.    // Load index template
  132.  
  133.    if(!$index = file_get_contents('index.template.xml')){
  134.  
  135.       notify("Aborted: Couldn't load template");
  136.  
  137.    }
  138.  
  139.    // Replace the empty DIV from the template with the new HTML
  140.  
  141.    $index = str_replace('<div id="headlines"/>', ('<div id="headlines">' . $html . '</div>'), $index);
  142.  
  143. // PUBLISH
  144.  
  145.    if(!file_put_contents("index.$ext", $index, LOCK_EX)){
  146.  
  147.       notify("Aborted: New index.$ext couldn't be written");
  148.  
  149.    }
  150.  
  151. // FINISHED
  152.  
  153.    echo notify("Published: New index.$ext written successfully from $rssURL");
  154.  
  155. ?>


index.template.xml
Code: [ Select ]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Title</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <link rel='stylesheet' type='text/css' href='screen.css' media="screen" />
</head>
<body>
    <div id="headlines"/>
</body>
</html>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5.     <title>Title</title>
  6.     <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  7.     <meta http-equiv="Content-Style-Type" content="text/css" />
  8.     <link rel='stylesheet' type='text/css' href='screen.css' media="screen" />
  9. </head>
  10. <body>
  11.     <div id="headlines"/>
  12. </body>
  13. </html>


screen.css
Code: [ Select ]
body{
    font-family: Verdana;
    font-size: 10px;
    background-color: #123456;
    color: #444;
}
div#headlines div{
    background-color: #fff;
    border: #000 1px solid;
    margin: 0.5em;
    padding: 1em;
}
div#headlines h3{
    font-size: 1.5em;
    color: #000;
    margin: 0em;
}
div#headlines a, div#headlines a:visited{
    color: #123456;
    text-decoration: underline;
    display: block;
    background-color: #f0f0f0;
    padding: 0.25em;
    font-weight: bold;
}
div#headlines a.permalink, div#headlines a.permalink:hover, div#headlines a.permalink:visited{
    display: inline;
    background-color: transparent;
    color: #123456;
}
div#headlines a:hover{
    background-color: #123456;
    color: #f0f0f0;
}
div#headlines b{
    color: #000;
}

/* Generic Classes */

.off{
    visibility: hidden;
}
  1. body{
  2.     font-family: Verdana;
  3.     font-size: 10px;
  4.     background-color: #123456;
  5.     color: #444;
  6. }
  7. div#headlines div{
  8.     background-color: #fff;
  9.     border: #000 1px solid;
  10.     margin: 0.5em;
  11.     padding: 1em;
  12. }
  13. div#headlines h3{
  14.     font-size: 1.5em;
  15.     color: #000;
  16.     margin: 0em;
  17. }
  18. div#headlines a, div#headlines a:visited{
  19.     color: #123456;
  20.     text-decoration: underline;
  21.     display: block;
  22.     background-color: #f0f0f0;
  23.     padding: 0.25em;
  24.     font-weight: bold;
  25. }
  26. div#headlines a.permalink, div#headlines a.permalink:hover, div#headlines a.permalink:visited{
  27.     display: inline;
  28.     background-color: transparent;
  29.     color: #123456;
  30. }
  31. div#headlines a:hover{
  32.     background-color: #123456;
  33.     color: #f0f0f0;
  34. }
  35. div#headlines b{
  36.     color: #000;
  37. }
  38. /* Generic Classes */
  39. .off{
  40.     visibility: hidden;
  41. }


Thoose three files are enough to get things started.
The first time it's accessed it's going to create at least one more file, which is a backup of the current index/page, or a new index/page itself.
If "human" isn't used, it's going to create one more file, "rss.log".

The "// REPLACEMENT" section of "build.php" should give you a pretty good idea of how things work if you have any experience with HTML/CSS/ect.

This is a decent page detailing the elements of RSS,
http://blogs.law.harvard.edu/tech/rss
  • northjersey78
  • Graduate
  • Graduate
  • User avatar
  • Posts: 173
  • Loc: New Jersey

Post 3+ Months Ago

OK, I created those 3 files and uploaded to my root directory. I only included the code you posted above.

When I try to access http://www.nysportsboard.com/build.php I get this:

Warning: copy(index.html.bak): failed to open stream: Permission denied in /home/nysports/public_html/build.php on line 17

Fatal error: Call to undefined function: file_put_contents() in /home/nysports/public_html/build.php on line 7


Here is line 17:

Code: [ Select ]
if(!copy("index.$ext", "index.$ext.bak")){


Here is line 7:

Code: [ Select ]
else{file_put_contents('rss.log', '[' . date('r') . "] $message\r\n", FILE_APPEND);exit;}
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Quote:
Fatal error: Call to undefined function: file_put_contents() in /home/nysports/public_html/build.php on line 7

That's where the "PHP 5" comes in. ;)

The permission denied is a matter of file permissions, somthing I don't deal with.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

I didn't really take the time to understand what's going on in the code, but Joebert is right about the permissions. The directory where the file is written to needs write permissions, in other words CHMOD 777. I wouldn't recommend writing the file to the root because you don't want to have your entire root directory writeable. Best theing would be create a separte directory for the "write" files and alter any paths in the code to that directory instead of the root.
  • northjersey78
  • Graduate
  • Graduate
  • User avatar
  • Posts: 173
  • Loc: New Jersey

Post 3+ Months Ago

OK, I found an example of what I am looking for:

http://weblogs.macromedia.com/mxna/

Something along those lines - several feed categores along the right side from which to choose, with the RSS feed updated automatically on the left. Maybe a rating/ email this/ discuss this in the forum link for each feed. Links to other areas of the site along the top. How involved is something like this?

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 44 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.