Advertisement
Guest User

PHP RSS script

a guest
Feb 7th, 2011
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. <?php
  2.  
  3. // Create a new DOM Document object, this will create a new XML declaration for us.
  4. // We can enter the version number and encoding (in that order), by passing
  5. // two arguments to the object's constructor.  By default, the XML
  6. // declaration's version attribute will be set to "1.0"
  7. $pDom = new DOMDocument();
  8.        
  9. // Here we create a new root elelement named rss.  
  10. $pRSS = $pDom->createElement('rss');
  11.  
  12. // We now add a new attribute to the rss element.  We name this new
  13. // attribute version and give it a value of 0.91.
  14. $pRSS->setAttribute('version', 0.91);
  15.  
  16. // Finally we append the attribute to the XML tree using appendChild
  17. $pDom->appendChild($pRSS);
  18.  
  19. // We repeat the same process again here, but this time we're creating
  20. // the channel element.
  21. $pChannel = $pDom->createElement('channel');
  22.  
  23. $pRSS->appendChild($pChannel);
  24.  
  25. // Create the main child nodes of channel, these contain the information
  26. // related to this RSS file.  I'm not going to comment each one of these
  27. // as they should be easy enough to understand.  Basically we're creating
  28. // a new element for each node, the first argument specifies the name of
  29. // the element we're creating, and the second specifies the text value
  30. // of the node, for example, title would render as:  <title>TalkPHP</title>
  31. $pTitle = $pDom->createElement('title', 'TalkPHP');
  32. $pLink  = $pDom->createElement('link', 'http://www.talkphp.com');
  33. $pDesc  = $pDom->createElement('description', 'Discuss PHP and other various web related topics in a knowledgeable and friendly community.');
  34. $pLang  = $pDom->createElement('language', 'en');
  35. $pImage = $pDom->createElement('image');
  36.  
  37. // Here we simply append all the nodes we just created to the channel node
  38. $pChannel->appendChild($pTitle);
  39. $pChannel->appendChild($pLink);
  40. $pChannel->appendChild($pDesc);
  41. $pChannel->appendChild($pLang);
  42. $pChannel->appendChild($pImage);
  43.  
  44. // Create three new elements that are needed to "describe" our image
  45. $pURL   = $pDom->createElement('url', 'http://www.talkphp.com/images/misc/rss.jpg');
  46. $pTitle = $pDom->createElement('title', 'TalkPHP');
  47. $pLink  = $pDom->createElement('link', 'http://www.talkphp.com');
  48.  
  49. // Append these new elements to the image element
  50. $pImage->appendChild($pURL);
  51. $pImage->appendChild($pTitle);
  52. $pImage->appendChild($pLink);
  53.  
  54. // This array represents the data contained within the imaginary database.
  55. $aLatestThreads = array
  56. (
  57.     array
  58.     (
  59.         'title' => "G'day",
  60.         'link' => 'http://www.talkphp.com/showthread.php?t=1107&amp;goto=newpost',
  61.         'description' => "Forum: Member Introductions
  62.                            Posted By: Shaun
  63.                            Post Time: 09-14-2007 at 01:15 PM"
  64.     ),
  65.    
  66.     array
  67.     (    
  68.         'title' => "AJAX File Uploader",
  69.         'link' => 'http://www.talkphp.com/showthread.php?t=1106&amp;goto=newpost',        
  70.         'description' => "Forum: Javascript, AJAX, E4X
  71.                            Posted By: CreativeLogic
  72.                            Post Time: 09-13-2007 at 07:32 PM",
  73.     ),
  74.    
  75.     array
  76.     (
  77.         'title' => "Some sites of mine...",
  78.         'link' => 'http://www.talkphp.com/showthread.php?t=1104&amp;goto=newpost',        
  79.         'description' => "Forum: Show Off
  80.                            Posted By: Craddock
  81.                            Post Time: 09-13-2007 at 04:54 PM"
  82.     )
  83.    
  84.  
  85. );
  86.  
  87. // Loop trough each result from our imaginary database, these are the
  88. // RSS items that the viewer of the RSS will see
  89. foreach ($aLatestThreads as $aThread)
  90. {
  91.     // Nothing new here, we're creating an item element as our parent
  92.     // and then creating and adding three child nodes to it.
  93.     $pItem  = $pDom->createElement('item');
  94.     $pTitle = $pDom->createElement('title', $aThread['title']);
  95.     $pLink  = $pDom->createElement('link', $aThread['link']);
  96.     $pDesc  = $pDom->createElement('description', $aThread['description']);
  97.    
  98.     // Append the nodes to the item, then append the item to the channel
  99.    
  100.     $pItem->appendChild($pTitle);
  101.     $pItem->appendChild($pLink);
  102.     $pItem->appendChild($pDesc);
  103.    
  104.     $pChannel->appendChild($pItem);
  105. }
  106.  
  107. // Set content type to XML, thus forcing the browser to render is as XML
  108. header('Content-type: text/xml');
  109.  
  110. // Here we simply dump the XML tree to a string and output it to the browser
  111. // We could use one of the other save methods to save the tree as a HTML string
  112. // XML file or HTML file.
  113. echo $pDom->saveXML();
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement