Skip to content

Instantly share code, notes, and snippets.

@zachbrowne
Created July 11, 2011 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachbrowne/1075295 to your computer and use it in GitHub Desktop.
Save zachbrowne/1075295 to your computer and use it in GitHub Desktop.
RSS Get
RSS_GET 1.48 By Aaron Dunlap
www. aarondunlap .com
==============
INSTALLATION
==============
1. Extract this zip (with folder names enabled)
2. Open rss_get.php and modify the settings toward the top of the script, the variables you may
change are indicated in comments.
3. Put rss_get.php in whatever directory you want. It's recomended that you put it in your site's root dir.
4. Put the "rss/" folder in the same dir as you put rss_get.php
5. CHMOD the "rss/" dir to 770 (or whatever you wish, so long as it's writeable and readable by scripts)
You may upload the example#.php files if you would like to see some samples.
Make sure to read through the opening comments of rss_get.php to learn about all the options you can set while calling the script.
==============
UTILIZATION
==============
In the file you wish to insert the RSS headlines into, you will need to include(); the rss_get.php
Example 1:
<HTML>
<head> <title> My RSS Feed </title> </head>
<body>
<?
include 'rss_get.php';
?>
</body>
</html>
This will output your DEFAULT rss feed (established by you in rss_get.php). If you want to add a different
RSS feed you need to set some variables before the include:
$url : The actual, full URL of the RSS feed
$diaplayname : the text to preceed the headlines, should be something like the name of that site
$number : How many headlines to show. If the number you have is more than the number of headlines on the feed,
it will include them all.
Example 2:
<HTML>
<head> <title> My RSS Feed </title> </head>
<body>
<?
$url = "http://games.slashdot.org/games.rss"; //URL for Slashdot Games
$displayname = "Slashdot | Games";
$number = 3;
include 'rss_get.php';
?>
</body>
</html>
You can include the script as many times on one page as you wish, and you can change the font formatting like so.
Example 3:
<HTML>
<head> <title> My RSS Feeds </title> </head>
<body>
<font face="Verdana" size="2">
<?
$url = "http://games.slashdot.org/games.rss"; //URL for Slashdot Games
$displayname = "Slashdot | Games";
$number = 3;
include 'rss_get.php';
?>
</font>
<br><br>
<font face="Arial" size="2">
<?
$url = "http://www.wilwheaton.net/mt/index.xml"; //URL for WilWheaton.Net's XML feed
$displayname = "Slashdot | Games";
$number = 3;
include 'rss_get.php';
?>
</font>
</body>
</html>
<HTML>
<head> <title> My RSS Feed </title> </head>
<body>
<?
include 'rss_get.php';
?>
</body>
</html>
<HTML>
<head> <title> My RSS Feed </title> </head>
<body>
<?
$url = "http://games.slashdot.org/games.rss"; //URL for Slashdot Games
$displayname = "Slashdot | Games";
$number = 3;
include 'rss_get.php';
?>
</body>
</html>
<HTML>
<head> <title> My RSS Feeds </title> </head>
<body>
<font face="Verdana" size="2">
<?
$url = "http://games.slashdot.org/games.rss"; //URL for Slashdot Games
$displayname = "Slashdot | Games";
$number = 3;
include 'rss_get.php';
?>
</font>
<br><br>
<font face="Arial" size="2">
<?
$url = "http://www.wilwheaton.net/mt/index.xml"; //URL for WilWheaton.Net's XML feed
$displayname = "Slashdot | Games";
$number = 3;
include 'rss_get.php';
?>
</font>
</body>
</html>
<head>
<style>
<!--
.displayname { font-family: Verdana; color: #000080; font-weight: bold;
border-top: 2px solid #000000; border-bottom: 2px solid #000000 }
.update { font-family: Comic Sans MS; font-size: 8pt; font-weight: bold;
border-left-style: solid; border-left-width: 1px;
border-right-style: solid; border-right-width: 1px;
border-top-width: 1px; border-bottom-width: 1px }
.headlinellink { text-decoration: none; font-family:Verdana; font-size:8pt; font-weight:bold }
:hover.headlinellink { text-decoration: none; font-size:10pt; font-family:Verdana; font-style:italic; font-weight:bold }
-->
</style>
<?
$url = "http://www.wilwheaton.net/mt/index.xml";
$displayname = "WilWheaton.net - WWdN";
$number = 10;
include "rss_get.php";
?>
<!---
=========================
RSS_Get by Aaron Dunlap
http://www.aarondunlap.com
=========================
aaron 'at' aarondunlap.com
=========================
Version 1.481 (05/04/05)
-->
<?php
#==========================
# The following line declares how long to wait before reloading the RSS feed.
# Keep in mind, many sites discourage people from pulling its feed more often than 30 minutes.
# For example, Slashdot.org may ban your server if it tries to load the rss feed too often.
# You may change the below line.
#==========================
$minutes = 30; //How often to update the image. 0 indicates that image updates every time it is called (heavy strain on bandwidth & server)
#==========================
# The next line is what directory to dump the htm files generated by this script.
# It should be relative to the location of this script, and it *MUST* have a trailing slash if it's a seperate dir.
# If you want the files to be dumped in the same dir as this script, set it to ""
# You may change the next line
#==========================
$filename = "rss/";
#==========================
# The next 4 lines are default settings for the script to use, if they are not set prior to including this script
# You may change the next 4 lines.
#==========================
$default_url = "http://www.aarondunlap.com/rss.php?mode=hl"; //URL for the RSS/XML feed you're subscribing to.
$default_displayname = "|| aarondunlap.com ||"; //Title to appear before headlines. Should be feed-specific
$default_number = 5; //How many headlines to display. If you set it higher than the ammount of headlines, it will stop there
$default_target = "_self"; //Target for headline links. Options: "_self" and "_blank"
#==========================
# The next line is whether or not the script should auto-update.
# If enabled, the script will check to see if a newer version of this script is available and will
# automatically download and install it. Your current version of the script will be backed up in your $filename
# directory.
# If you have manually edited this script, you should disable this. Otherwise, it is recomended that you enable it.
# Also note, your customized settings (default_url, default_displayname, filename, etc) WILL be kept during
# auto-updates. Alterations to the actual function code (beneath) would be overwritten in an update.
# During updates, your existing script is backed up into the $filename directory as "backup_[date]_(time).php"
# You may change the next line
#==========================
$autoupdate = TRUE;
#==========================
# The settings below are for advanced headline truncation.
# $default_trunc is how many characters to cut off at,
# the default is FALSE, which wont truncate
# at all.
# You may use $trunc when calling this script
# to define the truncation for specific feeds
# $default_delim is what character to cut off the headline at
# for example, if your headlines contain useless
# data at the end, like "(via Reuters)", setting
# the delimiter to "(" will stop that from showing.
# You may use $delim when calling this script
# to define the truncation for specific feeds
#===========================
$default_trunc = FALSE;
$default_delim = FALSE;
#XXXXXXXXXXXXXXXXXXXXXXXXXX
# Everything below is functional code.
# You should not change it unless you know what you're doing,
# and even if you do know what you're doing, are you sure you're
# awake enough to do it? Maybe take a nap, rethink things, try again.
#XXXXXXXXXXXXXXXXXXXXXXXXXX
$version = 1.481;
//If these variables aren't declared already, use defaults.
if (!isset($url)) { $url = $default_url; }
if (!isset($displayname)) { $displayname = $default_displayname; }
if (!isset($number)) { $number = $default_number; }
if (!isset($target)) { $target = $default_target; }
if (!isset($trunc)) { $trunc = $default_trunc; }
if (!isset($delim)) { $delim = $default_delim; }
//In-URL definitions. Cannot be used for including, but it works great for testing.
if (isset($_GET['url'])) { $url = $_GET['url'];}
if (isset($_GET['number'])) { $number = $_GET['number'];}
if (isset($_GET['displayname'])) { $displayname = $_GET['displayname'];}
if (isset($_GET['rssHeadline'])) { $rssHeadline = $_GET['rssHeadline'];}
$basefile = $filename;
$versionfile = $filename."updatelog.htm"; //File for update attempt log
$filename .= md5($url).".htm"; //Prepare filename for htm output
#==========================
# Script updating. Checks to see if an update attempt has been made in 24 hours, then goes ahead.
#==========================
if (((is_file($versionfile)) && (((time()-filemtime($versionfile)) >= (24 * 60 * 60))) || (!is_file($versionfile))) && ($autoupdate == TRUE)) {
$out = "";
//If updatelog.htm is too big, clear it out.
if (filesize($versionfile) > 102400) { unlink($versionfile); $out .= "<i>[".date("m/d/y H:i:s")."]</i> <b>Cleared update log because it was too big (>100kB)</b><hr>"; }
$fd = fopen ($versionfile , "a+");
$out .= "<i>[".date("m/d/y H:i:s")."]</i> Attempting to acquire latest version number: ";
fwrite ($fd , $out);
require 'http://www.aarondunlap.com/code/rss_get_version.php';
if ((isset($latestversion)) && (isset($versionURL))) {
$out = "<b>Acquired!</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Comparing version numbers: ";
fwrite ($fd , $out);
if ($version < $latestversion) {
$out = "<b>Your version ($version) is out-of-date. Updating to version $latestversion.</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Loading updated script: ";
fwrite ($fd , $out);
//Reads the new file from my server
if (@$newversfile = fopen($versionURL,"r")) {
while (!feof($newversfile)) {
$newvers .= fgets($newversfile);
}
//Transfers currently existing settings onto the update.
$newvers = str_replace("http://www.aarondunlap.com/rss.php?mode=hl",$default_url,$newvers);
$newvers = str_replace("|| aarondunlap.com ||",$default_displayname,$newvers);
$newvers = str_replace('$default_number = 5;','$default_number = '.$default_number.';',$newvers);
$newvers = str_replace('$autoupdate = TRUE;','$autoupdate = '.$autoupdate.';',$newvers);
$newvers = str_replace('$filename = "rss/";','$filename = "'.$basefile.'";',$newvers);
$newvers = str_replace('$minutes = 30;','$minutes = '.$minutes.';',$newvers);
$newvers = str_replace('$default_target = "_self";','$default_target = '.$default_target.';',$newvers);
$out = "<b>Opened</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Checking write-access in directory: ";
fwrite ($fd , $out);
//Checks to make sure we can write in this directory
if (is_writable(getcwd())) {
$out = "<b>Writable!</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Writing new version to temporary file: ";
fwrite ($fd , $out);
$outfile = fopen("rss_get_temp.txt","w+");
fwrite($outfile, $newvers);
fclose($outfile);
$out = "<b>Written.</b><br>\n<i>[".date("m/d/y H:i:s")."]</i> Backing up outdated file in $basefile directory:";
fwrite ($fd , $out);
//Backs up the current script (this one) into the rss folder
if (!copy("rss_get.php",$basefile."backup_".date("m-d-y_(H.i)").".php")) {
$out = "<b>Failed.</b><hr>\n";
fwrite ($fd , $out);
fclose($fd);
} else {
$out = "<b>Moved.</b><br>\n<i>[".date("m/d/y H:i:s")."]</i> Replacing script with new version:";
fwrite ($fd , $out);
//Renames the temp file as this file, effectively replacing it.
if (@!rename("rss_get_temp.txt","rss_get.php")) {
/*Dude, look at these if blocks!*/ $out = "<b>Failed.</b><hr>\n";
fwrite ($fd , $out);
fclose($fd);
} else {
chmod("rss_get.php",774);
$out = "<b>Replaced</b><br>\n<i>[".date("m/d/y H:i:s")."]</i><b> RSS_GET updating complete. </b>\n<br><i>[".date("m/d/y H:i:s")."]</i><b>Version note from author:</b>".$message."<hr>\n";
fwrite ($fd , $out);
fclose($fd);
}
}
} else {
$out = "<b>Not Writable.</b><hr>\n";
fwrite ($fd , $out);
fclose($fd);
}
} else {
$out = "<b>Failed.</b><hr>\n";
fwrite ($fd , $out);
fclose($fd);
}
} else {
$out = "<b>Your version is current</b><hr>\n";
fwrite ($fd , $out);
}
} else {
$out = "<b>Failed.</b><hr>\n";
fwrite ($fd , $out);
fclose($fd);
}
} // That's a lot of elses! Alright, we're done with the update thing.
#==========================
# Check the modify time of the htm file for this feed, and see if it's too new to reload the feed.
# If the file is too new, it loads the htm file. This stops the script from constantly pulling the feed.
#==========================
if (($minutes > 0) && (is_file($filename)) && (((time()-filemtime($filename)) < ($minutes * 60)))) {
include $filename;
$time = floor((time()-filemtime($filename)) / 60); //See how many "minutes ago" the file was made.
echo "<br><i><span class=\"updated\">Updated $time minutes ago.</span></i>"; //Include "minutes ago" after output.
} elseif (@fopen($url,"r")) { //Makes sure the file can actually be accessed.
#==========================
# If we're down here, it means that the feed needs to be reloaded.
#==========================
$rssHandle = fopen($url,"r") ; // Open the rss file for reading
while (!feof($rssHandle)) {
$rssData .= fgets($rssHandle);
}
#==========================
# Feed parsing
#==========================
$tag = "item ";
$rssData = preg_replace("/<" . $tag . "(.|\s)*?>/","<item>",$rssData);
$rssData = chop($rssData); // Strip any whitespace from the end of the string
$rssData = ereg_replace("[\r,\n]", "", $rssData); //Clear line breaks
$rssData = strstr($rssData,"<item>"); //Remove everything before <item>.
#==========================
# Strip specific tags and their content from the feed, to lighten the strain on the processor.
# Currently, only the <description></description> tags are stripped, we don't need them and sometimes
# they are REALLY long, getting rid of them now makes it easier to parse later.
#==========================
$tags_and_content_to_strip = Array("description");
foreach ($tags_and_content_to_strip as $tag) {
$rssData = preg_replace("/<" . $tag . ">(.|\s)*?<\/" . $tag . ">/","",$rssData);
}
$rssData = str_replace("<item>","", $rssData); //Remove <item> itself
$rssData = urldecode($rssData); //Replace any silly %20-type characters with their readable replacement.
$rssData = str_replace(strstr("</channel>",$rssData),"",$rssData);
$rssArray = explode("</item>",$rssData); //Creates an Array from all the headlines
$title = array();
$link = array();
#==========================
# This loop creates an array for links and another for titles.
#==========================
$x = 0;
while($x < $number) {
$link[$x] = strstr($rssArray[$x],"<link>"); //Remove everything before <link>
$link[$x] = ereg_replace("<link>","",$link[$x]);
$link[$x] = str_replace(strstr($link[$x],"</link>"),"",$link[$x]);
$link[$x] = trim($link[$x]);
$title[$x] = strstr($rssArray[$x],"<title>");
$title[$x] = ereg_replace("<title>","",$title[$x]); // Remove the leading <title> tags from the selected headline
$title[$x] = str_replace(strstr($title[$x],"</title>"),"",$title[$x]); // Remove </title> and anything after it
$title[$x] = trim($title[$x]);
if ($trunc != FALSE) { $title[$x] = str_replace(substr($title[$x],$trunc),"",$title[$x]); }
if ($delim != FALSE) { $title[$x] = str_replace(strstr($title[$x],$delim),"",$title[$x]); }
if ($title[$x] == "") { $number = $x; break; } //If there are no more headlines, reset $number to the max.
$x++;
}
#==========================
# Writing the file
#==========================
$fp = fopen($filename, "w+");
$x=0;
fwrite($fp,"<b><span class=\"displayname\">$displayname</span></b> \n"); //Write the displayname to the file
while ($x < $number) { //This loop writes each line individualy.
fwrite($fp,"<br>\n-<a class=\"headlinellink\" target=\"$target\" href=\"$link[$x]\">$title[$x]</a>");
$x++;
}
fclose($fp);
include $filename;
echo "<br><i><span class=\"updated\">Live.</span></i>";
} else {
#==========================
# Error handling:
# (rss url given is unreadable)
#==========================
echo "<b>Could not connect to $url. </b>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment