LoginSignup
7
9

More than 5 years have passed since last update.

PHPでRSS1.0取得で躓いたので覚書。「dc:date」と「content:encoded」の取得方法

Last updated at Posted at 2013-06-03

RSS2.0だと普通に日付なども取れると思うのですが、
RSS1.0だと単純に file_get_contents('rssのURL')だと
dc:dateやcontent:encodedなど階層が深くなっている部分が取得できません。
ので、下記コードで取得してくることで実装しました。

<?php
  $rss_url = "RSSのURL";

  //RSSデータが取得できた場合に処理する。
  if($xml = file_get_contents($rss_url)){
    $xml = simplexml_load_string($xml);
    
    //RSSデータをループ
    foreach($xml->item as $item){
      //contentデータ取得
      $content = $item->children('http://purl.org/rss/1.0/modules/content/');
      //dcデータ取得
      $dc_date = $item->children('http://purl.org/dc/elements/1.1/');
    
      //取得したデータを変数に代入
      $data['date'] = date('Y年m月d日',strtotime($dc_date->date));
      $data['content'] = $content->encoded;
    }
  }
?>
7
9
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
7
9