Location: PHPKode > scripts > RSS_GET Headlines > rssget-headlines/rss_get.php

  RSS_Get by Aaron Dunlap
  aaron 'at' aarondunlap.com
  Version 2.1 (05/26/2010)


  // 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 = ''; //Title to appear before headlines. If left blank, it will be determined from the feed
  $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 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;

  // 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.

  //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'];}
  $filename .= md5($url).".htm"; //Prepare filename for htm output  

  // 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 ($ch = curl_init($url)) { //Makes sure the file can actually be accessed.
	//Read the feed
	$ch = curl_init($url);
	$data = curl_exec($ch);
	preg_match_all('/\<title[^>]*\>([^\>]+)\<\/title\>/',$data,$titles); //Load all <title>s into array
	preg_match_all('/\<link[^>]*\>([^\>]+)\<\/link\>/',$data,$links); //Load all <link>s into array

    if ($titles[1] != '')
	$titles = $titles[1];
	else $titles = array('Error','Could not process feed');
	if ($links[1]) $links = $links[1];
	else $links = array('','#');
	if ($displayname == '') $displayname = $titles[0];
	//Some feeds are set up so that the feed's <title> appears twice before the first story.. check for that
	if ($titles[0] == $titles[1]) array_shift($titles);
	for ($x=1;$x<=$number;$x++) {
		if ($trunc != FALSE) { $titles[$x] = substr($titles[$x],0,$trunc); }
		if ($delim != FALSE) { $titles[$x] = strstr($titles[$x],$delim,true); }
		$titles[$x] = strtr($titles[$x],array('<![CDATA['=>'',']]>'=>''));
		if ($titles[$x] == '') { $number = $x; break; }

	  // Writing the file
	  $fp = fopen($filename, "w+"); 
	  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=\"$links[$x]\">$titles[$x]</a>");
	 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>"; 


Return current item: RSS_GET Headlines