问题
I get a URL from a user. I need to know:
a) is the URL a valid RSS feed?
b) if not is there a valid feed associated with that URL
using PHP/Javascript or something similar
(Ex. http://techcrunch.com fails a), but b) would return their RSS feed)
Edit: See my answer below
回答1:
Found something that I wanted:
Google's AJAX Feed API has a load feed and lookup feed function (Docs here).
a) Load feed provides the feed (and feed status) in JSON
b) Lookup feed provides the RSS feed for a given URL
Theres also a find feed function that searches for RSS feeds based on a keyword.
Planning to use this with JQuery's $.getJSON
回答2:
The Zend Feed class of the Zend-framework can automatically parse a webpage and list the available feeds.
Example:
$feedArray = Zend_Feed::findFeeds('http://www.example.com/news.html');
回答3:
This link will allow you to validate the link against the RSS/Atom specifications using the W3C specs, but does require you to manually enter the url.
There are a number of ways to do this programmatically, depending on your choice of language - in PHP, parsing the file as valid XML is a good way to start, then compare it to the relevant DTD.
For b), if the link itself isn't a feed, you can parse it and look for a specified feed in the <head> section of the page, searching for a link whose type is "application/rss+xml", e.g:
<link rel="alternate" title="RSS Feed"
href="http://www.example.com/rss-feed.xml" type="application/rss+xml" />
This type of link is the one used by most browsers to "auto-discover" feeds (causing the RSS icon to appear in your address bar)
回答4:
a) Retrieve it and try to parse it. If you can parse it, it's valid.
b) Test if it's an HTML document (server sent text/html
) MIME-type. If so, run it through an HTML parser and look for <link>
elements with RSS feed relations.
回答5:
For Perl, there is Feed::Find , which does automate the discovery of syndication feeds from the webpage. The usage is quite simplicistic:
use Feed::Find;
my @feeds = Feed::Find->find('http://example.com/');
It first tries the link
tags and then scans the a
tags for files named .rss
and something like that.
回答6:
Are you doing this in a specific language, or do you just want details about the RSS specification?
In general, look for the XML prolog:
<?xml version="1.0" encoding="UTF-8"?>
followed by an <rss> element, but you might want to validate it as XML, fully validate it against a DTD, or verify that - for example, each URL referred to is valid, etc. More detail would help.
UPDATE: Ah - PHP. I've found this library to be pretty useful: MagpieRSS
来源:https://stackoverflow.com/questions/61535/how-to-discover-rss-feeds-for-a-given-url