Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

XML_Feed_Parser 1.0.0リリース


今まではPEAR::XML_RSSでしたが、今後はこれでしょうかね?RSS0.9x/RSS1.x/RSS2.0/Atomに対応しているようです。


XML_Feed_Parser is a parser for (the various) RSS and Atom format XML feeds. It attempts to provide a somewhat unified API while still allowing access to the full details of each feed type.

ということで、http://www.doyouphp.jp/sample/sample_xml_pear_rss.shtmlを元ネタに(RSS1.0/2.0だけですが)ざっと試してみました。以下コードです(手抜きだ。。。)。

<?php
require_once 'XML/Feed/Parser.php';
?>
<?php
function buildFeedList($url) {
    $source = file_get_contents($url);
    try {
        $feed = new XML_Feed_Parser($source);
    } catch (Exception $e) {
        die($e->getMessage());
    }

    $version = $feed ->version();
    printf(
        '<h1><a href="%s">%s</a>のヘッドライン</h1>',
        $feed->link,
        mb_convert_encoding($feed->title, mb_internal_encoding(), 'auto')
    );
    echo '<ul>';
    foreach ($feed as $entry) {
        switch ($version) {
        case 'RSS 1.0':
            $dt = date('Y/m/d H:i',$entry->date);
            break;
        case 'RSS 2.0':
            $dt = date('Y/m/d H:i', $entry->pubDate);
            break;
        default:
            $dt = 'no data';
        }
        printf(
            '<li>[%s] <a href="%s" title="%s">%s</a></li>',
            $dt, 
            $entry->link,
            mb_convert_encoding($entry->title, mb_internal_encoding(), 'auto'),
            mb_convert_encoding($entry->title, mb_internal_encoding(), 'auto')
        );
    }
    echo '</ul>';
    echo '<hr/>';
}
buildFeedList('http://d.hatena.ne.jp/shimooka/rss');
buildFeedList('http://www.phppro.jp/news/rss.php');

$entry(XML_Feed_Parser_xxxxElementクラス)に各feedのマッピング情報を持っているようなので、日付とかは同じAPIで取り出したいなぁ。使い方がマズイのかな。。。