Advertisement
suebriquet

Sue's RSS Feed Generator

Sep 15th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.97 KB | None | 0 0
  1. <?php
  2. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3.     RSS Feed by Sue Feng
  4.     http://suefeng.net
  5.     Edit to your liking
  6. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  7.  
  8. /* edit this section
  9. -------------------------------*/
  10. $limit = 5; //number of recent entries
  11. $title = "My blog"; //name of your site
  12. $feedurl = "http://mysite.com/rss.php"; //feed url
  13. $description = "Here's my blog on life and other ramblings. Enjoy!"; //blog description
  14. $language = "en-us"; //language your entries are written in ie en-us for American English
  15. $imgurl = "http://mysite.com/images/rss.gif"; //image url
  16. $siteurl = "http://mysite.com/blog"; //site url
  17. $imgwidth = 90; //number of pixels wide your image is
  18. $imgheight = 36; //number of pixels tall your image is
  19. $dateformat = "D, d M Y H:i:s T"; //format of the timestamp
  20.  
  21. //this only works if your entries are linked based on id and use query strings
  22. $entryurl = "http://mysite.com/entry.php?id="; //the url of your blog post in query string format without the id
  23.  
  24. // database variables
  25. $host = "localhost";
  26. $user = "username";
  27. $pass = "password";
  28. $database = "databasename";
  29. $blogtable = "blog"; //name of the table your blog is in
  30.  
  31. /*  okay no more variables,
  32.     edit structure if you know what you're doing
  33.     change row['timestamp'], row['id'], row['title'], row['entry']
  34.     if those are not the row names you used.
  35. --------------------------------*/
  36.  
  37. $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
  38. mysql_select_db($database, $linkID) or die("Could not find database.");
  39. $query = "SELECT * FROM $blogtable ORDER BY timestamp DESC LIMIT $limit";
  40. $resultID = mysql_query($query, $linkID) or die("Data not found.");
  41.  
  42. header("Content-type: text/xml");
  43. $xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  44. $xml_output .= "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
  45. $xml_output .= "<channel>\n";
  46. $xml_output .= "<title>".$title."</title>\n";
  47. $xml_output .= "<atom:link href=\"".$feedurl."\" rel=\"self\" type=\"application/rss+xml\"/>\n";
  48. $xml_output .= "<link>".$blogurl."</link>\n";
  49. $xml_output .= "<description>".$description."</description>\n";
  50. $xml_output .= "<language>".$language."</language>\n";
  51.  
  52. // optional, delete if you don't need it
  53. $xml_output .= "<image>\n";
  54. $xml_output .= "<title>".$title."</title>\n";
  55. $xml_output .= "<url>".$imgurl."</url>\n";
  56. $xml_output .= "<link>".$siteurl."</link>\n";
  57. $xml_output .= "<width>".$imgwidth."</width>\n";
  58. $xml_output .= "<height>".$imgheight."</height>\n";
  59. $xml_output .= "</image>\n";
  60. // end optional
  61. for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
  62.     $row = mysql_fetch_assoc($resultID);
  63.     $timestamp = strtotime($row['timestamp']);
  64.     $timestamp = date($dateformat, $timestamp);
  65.     $xml_output .= "\t<item>\n";
  66.     $xml_output .= "\t\t<title>" . $row['title'] . "</title>\n";
  67.     $xml_output .= "\t\t<link>".$entryurl . $row['id'] . "</link>\n";
  68.     $xml_output .= "\t\t<pubDate>" . $timestamp . "</pubDate>\n";
  69.     $id = $row['id'];
  70.     $xml_output .= "<guid isPermaLink=\"false\">".$entryurl.$id."</guid>";
  71.         // Escaping illegal characters
  72.         $row['entry'] = str_replace("&", "&amp;", $row['entry']);
  73.         $row['entry'] = str_replace("\"", "&quot;", $row['entry']);
  74.         $description = strip_tags($row['entry'], '<br />');
  75.         $row['entry'] = str_replace("<", "&lt;", $row['entry']);
  76.         $row['entry'] = str_replace(">", "&gt;", $row['entry']);
  77.         $len = strlen($description);
  78.         $delim = " [...]";
  79.         $n = 200;
  80.         if ($len > $n) {
  81.             preg_match('/(.{' . $n . '}.*\s)\b/', $description, $matches);
  82.             $description = rtrim($matches[1]) . $delim;
  83.         }
  84.         else {
  85.             $description = $description;
  86.         }
  87.     $xml_output .= "\t\t<description>" . $description . "</description>\n";
  88.     $xml_output .= "\t\t<content:encoded>" . $row['entry'] . "</content:encoded>\n";
  89.     $xml_output .= "\t</item>\n";
  90. }
  91.  
  92. $xml_output .= "</channel>";
  93. $xml_output .= "</rss>";
  94.  
  95. echo $xml_output;
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement