PHPで外部サイトのRSSを取得する方法

投稿者: | 2017年1月28日

PHPで外部サイトのRSSを取得する方法です

■RSS1.0の場合

<?php

// RSS1.0の場合、
// <dc:date>をchildren("http://purl.org/dc/elements/1.1/")->dateで取得する

$xml = simplexml_load_file("*****");   // RSSのURL
$dc = "http://purl.org/dc/elements/1.1/";

foreach ($xml->item as $item) {
   $link         = $item->link;
   $title        = $item->title;
   $description  = $item->description;
   $dc_date      = date('Y.m.d', strtotime($item->children($dc)->date));

   echo $link . "<br>";
   echo $title . "<br>";
   echo $description . "<br>";
   echo $dc_date . "<br>";
}

?>

■RSS2.0の場合

<?php

// RSS2.0の場合
// 

$xml = simplexml_load_file("*****");   // RSSのURL

foreach ($xml->channel->item as $item) {
   $link         = $item->link;
   $title        = $item->title;
   $description  = $item->description;
   $pubDate      = $item->pubDate;

   echo $link . "<br>";
   echo $title . "<br>";
   echo $description . "<br>";
   echo $pubDate . "<br>";
}

?>