ブログなどのRSSをphpで一覧表示する。
<ul>
<?php
$pstsAtomTree = simplexml_load_file('http://spiral-records.com/psts/atom.xml');
for($i=0;$i<3;$i++){
print('<li><a href="'.$pstsAtomTree->entry[$i]->link[href].'">'.$pstsAtomTree->entry[$i]->title.'</a></li>');
}
?>
</ul>
phpにはsimplexml_load_file()という便利な関数がある。
上記例は、これを用いてこのブログのxmlを3件表示するためのコード。
RSSは吐き出されるデータが色々と違う(階層構造など)ので、RSSの中身を見ながらちょいちょいこのコードを修正すればOK。
この例の場合は
$pstsAtomTree->entry[$i]->link[href]
がリンクURLの部分で、
atom.xmlでは
<feed xmlns="http://www.w3.org/2005/Atom">
〜省略〜
<entry>
<title>データの有無や判別</title>
<link rel="alternate" type="text/html" href="http://www.spiral-records.com/psts/2011/12/post_151.html"/>
<id>tag:www.spiral-records.com,2011:/psts//6.1305</id>
<published>2011-12-26T07:13:54Z</published>
<updated>2011-12-27T01:38:34Z</updated>
<summary>is_null()とisset()は変数の値が長さ0の「""」でもtrueになっ...</summary>
<author>
<name>hrbys</name>
</author>
<category term="php" scheme="http://www.sixapart.com/ns/types#category"/>
<content type="html" xml:lang="ja" xml:base="http://www.spiral-records.com/psts/">
<〜コンテンツの中身省略〜
</content>
</entry>
という感じで階層構造になっているので、階層の通りに指定する。
URLはlinkタグのhrefなので、
link[href]
となる。
あああ、メタクソ便利。
