Creating A Dynamic OPML File

17 Mar

When I created Autism Hub I wanted to give its users as many options as possible. Obviously, rolling my own RSS feed was a priority but I also wanted to create an OPML file for people to import all the blog details straight into their readers.

Its an easy process – first declare the structure of your OPML file:

$top= "n"
      . "n"
      . "n"
      . "autismhub.opmln"
      . "" . date("D, j M Y H:i:s") . "n"
      . "Kevin Leitchn"
      . ""
      . "n"
      . "n";

$bottom = "n"
      . "";

$data = "";

$data will be used to build up our feed details. Again, this is easy. I have a database table which contains all my feed details. All you need to do is connect to this table in the usual way and loop through like so:

if ($row = mysql_fetch_array($sql)) {
  do {	
   $blogurl = $row['blogurl'];
   $feedurl = $row['feedurl'];	
   $name = htmlentities($row['name'], ENT_QUOTES);			
   $data .= "n";
 while ($row = mysql_fetch_array($sql));	
$all = $top . $data . $bottom;

Now, all you need to do is check for the presence of a file called autismhub.opml and if it exists, empty it of data and write new all the data to it. If it doesn’t exist, create it and then append all the data into it.

$file = "autismhub.opml";   
if (!$file_handle = fopen($file,"w+")) { echo "Cannot open file"; }  
if (!fwrite($file_handle, $all)) { echo "Cannot write to file"; }  

$filename = "/path/to/file/autismhub.opml";
$filename = realpath($filename);

if (!file_exists($filename)) {
 die("NO FILE HERE");

and finally, present this compiled file as a download:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename="".basename($filename)."";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
@readfile("$filename") or die("File not found.");

You can get all the code here.

%d bloggers like this: