LoginSignup
1
2

More than 5 years have passed since last update.

PHP simplexml_load_fileでRSS1.0を取得する

Posted at

PHPでRSS取得してみた

「RSSやATOMフィードがあれば欲しいな~、なければ空文字でいいよ」ってことで作ることになった。

最初にやったこと

simplexml_load_file使えば楽勝じゃんと思ってこんなコードを書いた。

    /**
     * RSSを取得する
     * @param string $rss RSS URL
     * @return SimpleXMLElement|string
     */
    public function getRss($rss)
    {
        $xml = '';
        // RSSのURLがありならRSS取得
        if ($rss) {
            $xml = simplexml_load_file($rss);
        }
        return $xml;
    }

getRssとかいってるけどATOMでも大丈夫だぜー!

RSS1.0

「RSS1.0のdc:dateが取れてないよ」ってことでちょっと追加した。

    /**
     * RSSを取得する
     * @param string $rss RSS URL
     * @return SimpleXMLElement|string
     */
    public function getRss($rss)
    {
        $xml = '';
        // RSSのURLがありならRSS取得
        if ($rss) {
            $xml = simplexml_load_file($rss);
            if ($xml->item) {
                // RSS1.0
                foreach ($xml->item as $entry) {
                    $dc = $entry->children('http://purl.org/dc/elements/1.1/');
                    $date = $dc->date;
                    $entry['date'] = $date;
                }
            }
        }
        return $xml;
    }

こんな感じになった。

item: [
    {
        @attributes: {
            date: "2013-10-04T00:45:00+09:00"
        },
        link: "リンク",
        title: "タイトル",
        description: "デスクリプション"
    },
]

ひとまずOK。よっしゃよっしゃ。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2