LoginSignup
16
18

More than 5 years have passed since last update.

RSSの最新情報だけを取得(修正&加筆版)

Last updated at Posted at 2015-04-02

RSS2.0の最新情報を取得
に頂いたコメントを受けて修正&RSS1.0Atom版も追加

RSS.php
    $RSS = simplexml_load_file($url);

    if( $RSS->channel->item[0] ){//RSS2.0

        $site_name = (string)$RSS->channel->title;
        $item  = $RSS->channel->item[0];
        $title = (string)$item->title;
        $link  = (string)$item->link;
        $date  = (string)$item->pubDate;

    }elseif( $RSS->item[0] ){//RSS1.0

        $site_name = (string)$RSS->channel->title;
        $item  = $RSS->item[0];
        $title = (string)$item->title;
        $link  = (string)$item->link;
        //dc:dateのデータはそのままでは取得できないのでchildrenメソッドを呼ぶ
        $date  = (string)$item->children('http://purl.org/dc/elements/1.1/')->date;

    }elseif( $RSS->entry[0] ){//Atom

        $site_name = (string)$RSS->title;
        $item  = $RSS->entry[0];
        $title = (string)$item->title;
        $link  = (string)$item->link->attributes()->href;
        $date  = (string)$item->published;

    }

複数サイトから取得するため、サイト名も取得するようにしました。
日付のところは実際の表示の際は

$date = date("Y.m.d",strtotime($date));

として表示させています。

16
18
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
16
18