0

I need to add a new item element to my RSS file, via PHP, without just generating the RSS from PHP. I know this will require removing older items, to match the number that I want displayed, but I don't know how to add them to the file in the first place.

My code looks somewhat like this:

<rss version="2.0">
  <channel>
    <title>My Site Feed</title>
    <link>http://www.mysitethathasfeed.com/feed/</link>
    <description>
        A nice site that features a feed.
    </description>
    <item>
        <title>Launched!</title>
        <link>http://www.mysitethathasfeed.com/feed/view.php?ID=launched</link>
        <description>
           We just launched the site! Come join the celebration!
        </description>
    </item>
  </channel>
</rss>
7
  • What do you mean by "without generating RSS"? Sounds like you will need to parse the existing document, manipulate it, then re-generate and output the RSS. Commented Mar 3, 2011 at 2:38
  • What I mean is that the content is not generated when accessed. The RSS is added when it needs to be, then not PHP processing needs to happen when the RSS is accessed. Commented Mar 3, 2011 at 3:00
  • @Tanner: I understand what you are going for here - a static RSS file which is manipulated by PHP and then resaved as a static file as opposed to a file which is dyanmically created on-the-fly by the execution of a PHP script. Is there any reason you are taking this approach? Generating the RSS output on-the-fly through the execution of a PHP script is a much better way to manage dynamic content. Commented Mar 3, 2011 at 3:36
  • Other alternatives: caching (generate the RSS on the fly, but cache it for some time before actually regenerating), publishing (generate and overwrite the static file whenever something changes). Parsing an existing file for manipulation is about the hardest and most error prone way to do this. Commented Mar 3, 2011 at 3:50
  • @Lucanos: Really? The app to be using this would store, at most, 5 item elements anyway. I can see how using dynamic RSS from PHP would be better for larger things, but is it really necessary for something this small? @deceze: I will look into cache-ing. Thanks. Commented Mar 3, 2011 at 4:23

2 Answers 2

1
// Load the XML/RSS from a file.
$rss = file_get_cotents('path_to_file');
$dom = new DOMDocument();
$dom->loadXML($rss);

Use http://php.net/manual/en/book.dom.php to learn how to modify the dom you loaded.

Sign up to request clarification or add additional context in comments.

Comments

0

Extending the answer from Kyle (oh OOP inheritence), and references from the PHP Manual

<?php

$rss = file_get_contents('feed.rss');
$dom = new DOMDocument();
$dom->loadXML($rss);

// should have only 1 node in the list
$nodeList = $dom->getElementsByTagName('channel');

// assuming there's only 1 channel tag in the RSS file:
$nChannel = $nodeList->item(0);

// now create the new item
$newNode = $dom->createElement('item');
$newNode->appendChild($dom->createElement('title', 'a new title post'));
$newNode->appendChild($dom->createElement('link', 'http://www.mysitethathasfeed.com/feed/view.php?ID=launched'));
$newNode->appendChild($dom->createElement('description', 'This is the 2nd post of our feed.'));

// add item to channel
$nChannel->appendChild($newNode);
$rss = $dom->saveXML();
file_put_contents('feed.rss', $rss);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.