RSSとは、Webサイトの見出しや要約などのメタデータを構造化して記述する、XMLベースのフォーマットです。RSSとは【RDF Site Summary】(Really Simple Syndication) - IT用語辞典 e-Words
RSSには大別して2種類あり、これらに互換性はありません。
| 系統 | ベース | 遷移 |
|---|---|---|
| RSS 1.0系 | RDF |
|
| RSS 2.0系 | XML |
|
ファイルの拡張子は、RSSの系統ごとに
のように使用されてます。しかし拡張子にたいした意味はないので、これは好みのもので構いません。それよりも、MIMEタイプをapplication/rss+xmlと返すべきです。
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>チャンネル名</title> <!-- (必須) -->
<link>http://example.com/</link> <!-- (必須) チャンネルに対応するWebサイトのURL -->
<description>チャンネルの説明</description> <!-- (必須) -->
<item>
<title>記事1のタイトル</title>
<link>http://example.com/item1.html</link> <!-- 記事のURL -->
<description>記事1の説明</description>
</item>
<item>
<title>記事2のタイトル</title>
</item>
<item>
<description>記事3の説明</description>
</item>
</channel>
</rss>
RSS 2.0のサンプル
| 要素 | 説明 | 必須 |
|---|---|---|
| title | ○ | |
| link | チャンネルに対応するWebサイトのURL | ○ |
| description | ○ | |
| item | × |
| 要素 | 説明 | 必須 |
|---|---|---|
| title | × | |
| link | 記事のURL | × |
| description | × |
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="http://example.com/sample.rss">
<title>チャンネル名</title> <!-- (必須) -->
<link>http://example.com/</link> <!-- (必須) チャンネルに対応するWebサイトのURL -->
<description>チャンネルの説明</description> <!-- (必須) -->
<items> <!-- (必須) コンテンツのRDFテーブル -->
<rdf:Seq>
<rdf:li resource="http://example.com/item1.html" />
<rdf:li resource="http://example.com/item2.html" />
<rdf:li resource="http://example.com/item3.html" />
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://example.com/item1.html">
<title>記事1のタイトル</title> <!-- (必須) -->
<link>http://example.com/item1.html</link> <!-- (必須) 記事のURL -->
<description>記事1の説明</description>
</item>
<item rdf:about="http://example.com/item2.html">
<title>記事2のタイトル</title>
<link>http://example.com/item2.html</link>
</item>
<item rdf:about="http://example.com/item3.html">
<title>記事2のタイトル</title>
<link>http://example.com/item3.html</link>
</item>
</rdf:RDF>
RSS 1.0のサンプル
| 要素 | 説明 | 必須 |
|---|---|---|
| title | ○ | |
| link | チャンネルに対応するWebサイトのURL | ○ |
| description | ○ | |
| items | コンテンツのRDFテーブル | ○ |
| 要素 | 説明 | 必須 |
|---|---|---|
| title | ○ | |
| link | 記事のURL | ○ |
| description | × |
RSSはXMLを基本としているため、RSSの解析はXMLのそれと同じです。
PHPでは、XMLのパーサを使用することで簡単に処理できます。たとえばRSS 2.0を対象とするならば、次のようにすることでその内容を出力できます。
$rss = simplexml_load_file( $url ); echo $rss->channel->title; // チャンネルのタイトル foreach( $rss->channel->item as $item ) { echo $item->title; // 項目のタイトル }
実際に使用されている例を見るのが、RSSを理解する助けとなります。