Posted By

antpaw on 01/28/09


Tagged

php xml


Versions (?)

Who likes this?

11 people have marked this snippet as a favorite

atma
umang_nine
jackkeller
nb109
vali29
jealousy

ninjacharlee
fadlee
ringo380
tux-world


OOP RSS/XML class for PHP


 / Published in: PHP
 

this will give you a lovely RSS.

Im sure you will need to edit the database stuff and row names

  1. <?php
  2.  
  3. mysql_connect("localhost","root","root") or die (mysql_error());
  4.  
  5. class RSS {
  6.  
  7. var $XMLdump;
  8.  
  9. var $pagetitle;
  10. var $pagelink;
  11. var $pegedescription;
  12. var $pagelanguage;
  13.  
  14. var $sqlresult;
  15.  
  16. function setHead($setPagetitle, $setPagelink, $setPegedescription, $setPagelanguage){
  17. $this->pagetitle = $setPagetitle;
  18. $this->pagelink = $setPagelink;
  19. $this->pegedescription = $setPegedescription;
  20. $this->pagelanguage = $setPagelanguage;
  21. }
  22.  
  23. function getDataFrom($setSQL){
  24. $this->sqlresult = mysql_query($setSQL);
  25. }
  26.  
  27.  
  28. function rssHead(){
  29. $this->XMLdump = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  30. <rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom/\">
  31. <channel>
  32. <title>".$this->pagetitle."</title>
  33. <link>".$this->pagelink."</link>
  34. <description>".$this->pegedescription."</description>
  35. <language>".$this->pagelanguage."</language>
  36. <lastBuildDate>".date("r", time())."</lastBuildDate>\n";
  37. }
  38.  
  39. function rssItems(){
  40. while($bla = mysql_fetch_assoc($this->sqlresult)){
  41. $this->XMLdump .= " <item>\n";
  42. $this->XMLdump .= " <title>".$bla['title']."</title>\n";
  43. $this->XMLdump .= " <link>http://bestnewssiteever.com/news/".$bla['id']."/</link>\n";
  44. $this->XMLdump .= " <category>".$bla['category']."</category>\n";
  45. $this->XMLdump .= " <pubDate>".date("r",$bla['pubDate'])."</pubDate>\n";
  46. preg_match_all("/^(?:[^.]*\.){3}/", $bla['content'], $trimedContent);
  47. $this->XMLdump .= " <description>".$trimedContent[0][0]."..</description>\n";
  48. $this->XMLdump .= " </item>\n";
  49. }
  50. }
  51.  
  52. function rssFooter(){
  53. $this->XMLdump .= " </channel>
  54. </rss>";
  55. }
  56.  
  57. function writeXML(){
  58. $this->rssHead();
  59. $this->rssItems();
  60. $this->rssFooter();
  61. return $this->XMLdump;
  62. }
  63.  
  64. function saveXML($file){
  65. $fp = fopen($file,"w+");
  66. flock($fp,2);
  67. fwrite($fp,$this->writeXML());
  68. flock($fp,3);
  69. fclose($fp);
  70. }
  71. }
  72.  
  73. $Bar = new RSS();
  74. $Bar->getDataFrom("SELECT * FROM news ORDER BY pubDate DESC");
  75. $Bar->setHead("TITLE","http://domain.de","DESCRIPTION","en-EN");
  76. $Bar->saveXML("blub.xml");
  77.  
  78. ?>

Report this snippet  

You need to login to post a comment.