如何使用多个RSS源制作RSS源?

时间:2017-08-31 18:36:55

标签: php wordpress rss

a closed Stack post中挑选,如何修改它以使用多个RSS源并从大量(5-10)RSS地址打印最新帖子的列表?添加更多逗号分隔不起作用。

(注意:此代码仍适用于WordPress博客,2017年8月)

<?php
$rss = new DOMDocument();

$rss->load('http://the .rsss or .xmls');
$feed = array();

foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        'image' => $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url')
        );

    array_push($feed, $item);
}
?>

1 个答案:

答案 0 :(得分:0)

您可以将rss url放在一个数组中,并按如下方式迭代它们:     

  $rss_urls = array(
    'http://firsturl.rss',
    'http://secondurl.rss',
    'http://thirdurl.rss',
  );


  $feed = array();

  foreach($rss_urls as $rss_url){

    $rss = new DOMDocument();
    $rss->load($rss_url);

    foreach ($rss->getElementsByTagName('item') as $node) {
      $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        'image' => $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url')
      );

      array_push($feed, $item);
    }
  }
?>