Code example for manually generating comment RSS
Recently, the discussion atmosphere in the blog park has been lively, and valuable comments have emerged one after another. Sometimes we would like to subscribe to the RSS comments of a certain article, but unfortunately the blog park does not currently have this function. For registered users, we can click the "Subscribe to Reply" link below the comment box to receive emails when new comments appear. It's a pity that anonymous users have to refresh constantly to pay attention to what has been discussed recently. But who are we? We are programmers, and this obstacle should be nothing more than a trivial matter for us. Build your own site, get page data, analyze HTML, and output it as RSS, it's that simple.
Lao Zhao gave the simplest example for this. You can subscribe to the comments of any article on http://jeffreyzhao.cnblogs.com. Because it is just a simple personal tool program, it does not consider performance, scalability, scalability, fault tolerance, and discards any unit testing, dependency injection and other "best practices" . In a word, it comes as easy as it comes.
This example is composed of two groups. The first part is a static HTML page that generates based on the article URL and forwards to its RSS link. Just a few lines of HTML and JavaScript:
<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>博客园RSS</title> <script language="javascript" type="text/javascript"> function goToCommentRss(url) { window.location = "CommentRss.ashx?url=" + encodeURIComponent(url); } </script> </head> <body> <textarea id="url" cols="50" rows="10"></textarea><br /> <input type="button" value="Comment RSS" onclick="goToCommentRss(document.getElementById('url').value)" /> </body> </html>
CommentRss.ashx will output the RSS of the comment for us. Its code is as follows:
public class CommentRss : IHttpHandler{ public void ProcessRequest(HttpContext context) { string url = context.Request.QueryString["url"]; WebClient webClient = new WebClient(); webClient.Encoding = Encoding.UTF8; string html = webClient.DownloadString(url); context.Response.ContentType = "text/xml"; context.Response.ContentEncoding = Encoding.UTF8; SyndicationFeed feed = GetRssFeed(url, html); Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed); XmlWriter rssWriter = XmlWriter.Create(context.Response.Output); rssFormatter.WriteTo(rssWriter); rssWriter.Close(); } private static SyndicationFeed GetRssFeed(string url, string html) { ... } public bool IsReusable { get { return false; } } }
In fact, the .NET Framework has prepared too many useful tools for us, we just need to splice them together. For example, with the WebClient class, three lines of code can download the HTML of the page. Then we obtain a SyndicationFeed object through the GetRssFeed method, and then output it through Rss20FeedFormatter. SyndicationFeed and Rss20FeedFormatter are both class libraries that come with .NET 3.5 and are placed in the System.ServiceModel.Syndicationnamespace# in the System.ServiceModel.dll assembly. ##, you can easily read or generate XML in Atom 1.0 or RSS 2.0 format for our use. For more information, please refer to this report on the InfoQ Chinese site: WCF's WebProgrammingModelResources.
The key to GetRssReed is to analyze the HTMLString, where Lao Zhao used regular expression to match the title, URL, time, user and content. Then constructing a SyndicationFeed object couldn't be simpler. Unfortunately, the HTML of different templates in the Blog Park is different, so this example of Lao Zhao only supports the current template. You can modify it yourself, for example, add a new parameter to CommentRss.ashx to specify the HTML parsing method, and then it can be used in multiple templates.
This example is also very simple to use. You can compile or deploy it on local IIS, open the Default.html page, and copy the URL of the article, such as "Old Zhao Talks about IL (2)" URL of an article:button and it will be linked to the RSS page. So it will be displayed in IE as:
RSS Efficient Getting Started Tutorial
The above is the detailed content of Code example for manually generating comment RSS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use PHP and XML to implement RSS subscription management and display on a website. RSS (Really Simple Syndication) is a standard format for publishing frequently updated blog posts, news, audio and video content. Many websites provide RSS subscription functions, allowing users to easily obtain the latest information. In this article, we will learn how to use PHP and XML to implement the RSS subscription management and display functions of the website. First, we need to create an RSS subscription to XM

With the rapid development of the Internet, more and more websites have begun to provide RSS subscription services, allowing users to easily obtain updated content from the website. As a popular server-side scripting language, PHP has many functions for processing RSS subscriptions, allowing developers to easily extract the required data from RSS sources. This article will introduce how to use PHP functions to obtain RSS subscription content. 1. What is RSS? The full name of RSS is "ReallySimpleSyndication" (abbreviated

How to write a simple RSS subscriber through PHP RSS (ReallySimpleSyndication) is a format used to subscribe to website content. Through the subscriber, you can get the latest articles, news, blogs and other updates. In this article, we will write a simple RSS subscriber using PHP to demonstrate how to obtain and display the content of an RSS feed. Confirm environment and preparation Before starting, make sure you have a PHP environment and have the SimpleXML extension installed.

How to use PHP to implement RSS subscription function RSS (ReallySimpleSyndication) is a format used to publish and subscribe to website updated content. Using RSS, users can easily obtain the latest information from websites that interest them without having to visit the website regularly. In this article, we will learn how to implement RSS subscription functionality using PHP. First, we need to understand the basic structure of RSS. A typical RSS document consists of one or more items

As Internet content continues to enrich and diversify, more and more people are beginning to use RSS technology to subscribe to blogs, news and other content they are interested in, so that they will no longer miss any important information. As one of the commonly used programming languages in web development, PHP also provides some powerful functions and tools to help us crawl RSS subscriptions from other websites and display them on our own website. This article will introduce how to use PHP to crawl RSS subscriptions of other websites and parse them into arrays or objects.

XML/RSS data integration can be achieved by parsing and generating XML/RSS files. 1) Use Python's xml.etree.ElementTree or feedparser library to parse XML/RSS files and extract data. 2) Use ElementTree to generate XML/RSS files and gradually add nodes and data.

Using PHP and XML to implement RSS subscription function RSS (ReallySimpleSyndication) is a standard format for publishing and subscribing to website updates. It is based on XML and gets the latest content through the subscriber's RSS reader. In this article, we will introduce how to use PHP and XML to implement a simple RSS subscription function. Create an XML file First, we need to create an XML file to store the content we want to publish. Suppose we want to publish a text

<ul><li><strong>Click to enter:</strong>ChatGPT tool plug-in navigation</li></ul><p>In<em>Influence</em> In the age of bloggers, vloggers, and content creators, every new way to follow our favorite influencers matters<em>
