Here's what I want to do.
- northjersey78
- Graduate


- Joined: Oct 31, 2003
- Posts: 173
- Loc: New Jersey
- Status: Offline
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?
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


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
April 28th, 2006, 6:04 am
- joebert
- Sledgehammer


- Joined: Feb 10, 2004
- Posts: 13455
- Loc: Florida
- Status: Offline
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.
index.template.xml
screen.css
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
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");
?>
// 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");
?>
- <?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");
- ?>
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>
<!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>
- <?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>
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;
}
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;
}
- 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;
- }
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
Strong with this one, the sudo is.
- northjersey78
- Graduate


- Joined: Oct 31, 2003
- Posts: 173
- Loc: New Jersey
- Status: Offline
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:
Here is line 7:
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
- Sledgehammer


- Joined: Feb 10, 2004
- Posts: 13455
- Loc: Florida
- Status: Offline
- ATNO/TW
- Super Moderator


- Joined: May 28, 2003
- Posts: 23404
- Loc: Woodbridge VA
- Status: Offline
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.
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
- northjersey78
- Graduate


- Joined: Oct 31, 2003
- Posts: 173
- Loc: New Jersey
- Status: Offline
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?
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?
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 6 posts
- Users browsing this forum: roelof and 119 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
