Ralpharama Archive. The archive is almost 20 years old, this website is no longer maintained.

Home | New Writing | Old Writing | Cooking | Technical | Wine Making | Cocktails
   
 
Article

How to make an RSS feed with PHP

Hits 5017 | Created 2007-06-13 | Modified 2007-06-13

A quick and dirty file that produces a RSS feed made up of recently modified files (txt, html, whatever).

Copy the code, edit the php as required, then upload into the folder you want the feed to be produced in. It will take the most recent files (txt, html, whatever you select) and make a feed from it in the same folder called 'rss.xml'.

It can be made to redirect after doing it to another page, so it can be done seamlessly without any visible process.

Title of each feed item is the filename without suffix and with another string pattern stripped (if required).

No time stamp is produced for each item, but one for the feed header is.

\n\n";
		$files = $files . "\n";
		$files = $files . "\n";
		$files = $files . $title;
		$files = $files . "\n\n";
		$files = $files . "\n";
		$files = $files . $description;
		$files = $files . "\n\n";
			$files = $files . "\n";
			$files = $files . date("r");
			$files = $files . "\n";
		$files = $files . "\n";
		$files = $files . $homeURL;
		$files = $files . "\n\n";
		while ($i<=$numpages & $i<$numArticles)
		{
			$files = $files . "\n";
			$files = $files . "\n" . $ourdir[$i]->prettyName . "\n\n";
			$files = $files . "\n";
			$temp = getFile($ourdir[$i]->name);
			$temp = decode($temp);
			$temp = strip_tags($temp);
			$temp = str_replace(" ", " ", $temp);
			$temp = substr($temp, 0, $textLen) . "...";
			$files = $files . $temp . "\n\n";
			$files = $files . "\n";
			$files = $files . "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php?id=" . str_replace(" ","%20",$ourdir[$i]->name);
			$files = $files . "\n\n";
			$files = $files . "\n";
			$i++;
		}
		$files = $files . "\n";

		$handle = fopen("rss.xml", 'w+');
		fwrite($handle, $files);
		fclose($handle);
	}

	echo "Finished.  rss.xml";		
	// Remove this and add the redirect below, if required.
	// header("Location: index.php");

	// This gets the current dir structure and returns it as an array with two elements, name and time
	function getdir2($start, $search)
	{
		global $pageid;
		global $pagenum;
		global $numpages;
		global $fileType;
		global $toStrip;
		$count=0;
		if($dir=opendir($start))
		{
			while (($file=readdir($dir))!=FALSE)
			{
				if ($file!="." & $file!="..")
				{
					$url=$start . "/" . $file;
					if (strpos($file, $search)>-1)
					{
						$dirlist[$count]->time=filemtime($file);
						$dirlist[$count]->name=$file;
						$file=str_replace($fileType,"",$file);
						$file=str_replace($toStrip,"",$file);
						$dirlist[$count++]->prettyName=$file;
					}
				}
			}
			closedir($dir);
		}
		if ($dirlist!=NULL)
		{
			rsort($dirlist);
		
			for ($i=0; $i<$count; $i++)
			{
				if ($dirlist[$i]->name==$pageid) $pagenum=$i;
			}
			$numpages=$count-1;
		}
		return $dirlist;
	}

	// This reads in a file
	function getFile($f)
	{
		$files=" ";
		if (file_exists($f))
		{
			$handle = fopen($f, "r");
			$files = file_get_contents($f);
			fclose($handle);
		}
		return $files;
	}

	// Format text for RSS
	function decode($files)
	{
		$files=str_replace("&","&",$files);
		$files=str_replace("","GBP",$files);
		$files=htmlentities($files);
		return $files;
	}
?>




Make a Comment

Name
Comment


Antispam Code (always a number)

Enter Code Above
Submit

Note: All HTML will be removed except for bold and italics. Links will not display. Line breaks are added automatically. Use <pre>...</pre> for code.

Tags

This item has the following tags:
tech rss xml

Useful? Donate for hosting costs

All content copyright (c) Ralpharama