How do you generate an RSS feed?

后端 未结 8 2151
野的像风
野的像风 2021-02-04 17:59

I\'ve never done it myself, and I\'ve never subscribed to a feed, but it seems that I\'m going to have to create one, so I\'m wondering. The only way that seems apparent to me i

相关标签:
8条回答
  • 2021-02-04 18:41

    For PHP I use feedcreator http://feedcreator.org/

    <?php define ('CONFIG_SYSTEM_URL','http://www.domain.tld/');
    
    require_once('feedcreator/feedcreator.class.php');
    
    $feedformat='RSS2.0';
    
    header('Content-type: application/xml');
    
    $rss = new UniversalFeedCreator();
    $rss->useCached();
    $rss->title = "Item List";
    $rss->cssStyleSheet='';
    $rss->description = 'this feed';
    $rss->link = CONFIG_SYSTEM_URL;
    $rss->syndicationURL = CONFIG_SYSTEM_URL.'feed.php';
    
    
    $articles=new itemList();  // list of stuff
    foreach ($articles as $i) {   
        $item = new FeedItem();
        $item->title = sprintf('%s',$i->title);
        $item->link = CONFIG_SYSTEM_URL.'item.php?id='.$i->dbId;
        $item->description = $i->Subject;   
        $item->date = $i->ModifyDate;   
        $item->source = CONFIG_SYSTEM_URL;   
        $item->author = $i->User;
        $rss->addItem($item);
    }
    
    print $rss->createFeed($feedformat);
    
    0 讨论(0)
  • 2021-02-04 18:41

    There are two ways to approach this. The first is to create the RSS document dynamically on request. The second is to write to a static file when a relevant change happens. The latter is faster, but requires a call to update the feed in (likely) many places vs. just one.

    With both methods, while you could edit the document with only the changes, it's much simpler to just rewrite the whole document every time with the most recent (10-50) items.

    0 讨论(0)
  • 2021-02-04 18:44

    I'd say the answer is having an RSS feed be nothing more than another view of your data. This means that your rss feed is simply an xml representation of the data in your database. Readers will then be able to hit that specific url and get back the current information in your application.

    0 讨论(0)
  • 2021-02-04 18:51

    I've got good results from Magpie RSS. Set up the included cacheing and all you need to do is write the query to retrieve your data and send the result to Magpie RSS, which then handles the update frequency.

    I wouldn't be writing an RSS file, unless your server is under particularly heavy load - one query (or a series of queries that adds to an array) for updated stuff is all you need. Write the query/ies to be sorted by date and then limited by X and you won't need to worry about 'removing old stuff' either.

    0 讨论(0)
  • 2021-02-04 18:53

    Here's a simple ASP.NET 2 based RSS feed I use as a live bookmark for my localhost dev sites. Might help you get started:

    <%@ Page Language="C#" EnableViewState="false" %>
    <%@ OutputCache Duration="300" VaryByParam="none" %>
    
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Web.Security" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Xml" %>
    <%@ Import Namespace="System.Text" %>
    <%@ Import Namespace="System.DirectoryServices" %>
    
    <script runat="server">
    
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Specialized.StringCollection HideSites = new StringCollection();
        System.Collections.Generic.List<string> Sites = new System.Collections.Generic.List<string>();
    
        HideSites.Add(@"IISHelp");
        HideSites.Add(@"MSMQ");
        HideSites.Add(@"Printers");
    
        DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/W3SVC/1/ROOT");
        foreach (DirectoryEntry site in entry.Children)
        {
            if (site.SchemaClassName == "IIsWebVirtualDir" && !HideSites.Contains(site.Name))
            {
                Sites.Add(site.Name);
            }
        }
    
        Sites.Sort();
    
        Response.Clear();
        Response.ContentType = "text/xml";
        XmlTextWriter RSS = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        RSS.WriteStartDocument();
        RSS.WriteStartElement("rss");
        RSS.WriteAttributeString("version","2.0");
        RSS.WriteStartElement("channel");
        RSS.WriteElementString("title", "Localhost Websites");
        RSS.WriteElementString("link","http://localhost/sitelist.aspx");
        RSS.WriteElementString("description","localhost websites");
    
        foreach (string s in Sites)
        {
            RSS.WriteStartElement("item");
            RSS.WriteElementString("title", s);
            RSS.WriteElementString("link", "http://localhost/" + s);
            RSS.WriteEndElement();
        }
    
        RSS.WriteEndElement();
        RSS.WriteEndElement();
        RSS.WriteEndDocument();
        RSS.Flush();
        RSS.Close();
        Response.End();
    }
    
    </script>
    
    0 讨论(0)
  • 2021-02-04 18:57

    If you want to generate a feed of elements that already exist in HTML, one option is to modify your HTML markup to use hAtom (http://microformats.org/wiki/hAtom) and then point feed readers through an hAtom->Atom or hAtom->RSS proxy.

    0 讨论(0)
提交回复
热议问题