Symfony 如何解析 Twig 上的 RSS 提要

Symfony how to parse RSS feed on Twig

例如,在 PHP 中,解析 RSS 提要的方法可能是:

<?php
$rss = simplexml_load_file('http://blog.wordpress_site.com/feed/');

{{ rss }}

foreach ($rss->channel->item as $item) {
    echo $item->title;
    echo $item->link;
    echo $item->description;
    echo $item->guid;
}
?>

我怎样才能在 Twig 上拥有这个?

更新:感谢回复,我得到了它。现在它是按项目获取的,但不是 post:

的图像、类别或文本等某些字段
SimpleXMLElement {#955 ▼
  +"title": "Website. Description of the website"
  +"link": "http://blog.website.com/liktothepost"
  +"pubDate": "Fri, 17 Feb 2017 07:56:43 +0000"
  +"category": SimpleXMLElement {#1131}
  +"guid": "http://blog.website.com/?p=400"
  +"description": SimpleXMLElement {#953}
}

您将创建一个带有操作的控制器,以将对象传递给您要渲染的 Twig 文件,如下所示:

public function viewRSSAction(Request $request){
    $rss = simplexml_load_file('http://blog.wordpress_site.com/feed/');

    return $this->render('my_rss.html.twig', array(
            'rss' => $rss,
    ));
}

那么您的 my_rss.html.twig 可能如下所示:

{% for item in rss %}
    {{ item.title }}
    {{ item.link }}
    {{ item.description }}
    {{ item.guid }}
{% endfor %}