3

How do I go about creating an atom feed in PHP?

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
somejkuser
  • 8,856
  • 20
  • 64
  • 130
  • 3
    Have you done any research? This is a pretty decent article: http://www.ibm.com/developerworks/opensource/library/x-phpatomfeed/index.html – Ben Everard Nov 09 '09 at 14:45

3 Answers3

3

An update for anyone that may stumble upon this thread:

A very similar question was asked in The best PHP lib/class to generate RSS/Atom and it lead to a number of good lib/roll your own recommendations.

Community
  • 1
  • 1
labratmatt
  • 1,821
  • 2
  • 20
  • 21
0

Use a library.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Wikipedia has an example of what an ATOM feed looks like. Feel free to modify this very basic RSS class that I wrote a long while ago to create a very simple RSS feed:

class RSSFeed
{       
    var $feedHeader;
    var $feedItems;

    /* Class Constructor */
    function RSSFeed()
    {
        //do some contruction
        $this->feedHeader = '';
        $this->feedItems = '';
    }

    function setFeedHeader($title, $link, $description, $copyright, $lastBuildDate, $ttl)
    {
        $this->feedHeader = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel>';
        $this->feedHeader .= '<title>'.$title.'</title>';
        $this->feedHeader .= '<link>'.$link.'</link>';
        $this->feedHeader .= '<description>'.$description.'</description><copyright>'.$copyright.'</copyright>';
        $this->feedHeader .= '<language>en-GB</language><lastBuildDate>'.$lastBuildDate.' GMT</lastBuildDate><ttl>'.$ttl.'</ttl>';
    }

    function pushItem($title, $link, $description, $pubDateTime)
    {
        $item = '<item><title>' . htmlentities(stripslashes($title)) . '</title>';
        $item .= '<link>' . $link . '</link>';
        $item .= '<guid>' . $link . '</guid>';
        $item .= '<description>' . htmlentities(stripslashes($description)) . '</description>';

        $item .= '<pubDate>' . $pubDateTime . ' GMT</pubDate></item>';

        $this->feedItems = $item . $this->feedItems;
    }

    function writeOutFeed($path)
    {
        $file = fopen($path, "w");
        fputs($file, $this->feedHeader);
        fputs($file, $this->feedItems);
        fputs($file, '</channel></rss>');
        fclose($file);
    }
}
MalphasWats
  • 3,255
  • 6
  • 34
  • 40
  • You aren’t escaping virtually anything! What if there is a literal tag in the item title or description? The output will be borked. `O_O` – Alan H. Jun 15 '11 at 23:39