Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
9
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

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

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;
    }
  }
?>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
9
Help us understand the problem. What are the problem?