2017-02-24 109 views
5

RSS提要例如,在PHP的方式来解析RSS源可能是:的Symfony如何解析上的树枝

<?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; 
} 
?> 

我怎么能有这样的细枝?

UPDATE:感谢我的答复。现在,通过项目得到这一点,但不是像图像,类别或职位的文本的某些字段:

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} 
} 

回答

2

你会创建一个控制器与动作的对象传递给你的枝杈文件要呈现像这样:

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 %} 
+0

的感谢!根据你的回复,我终于用你的代码在一个扩展中做了一个Twig函数,谢谢!我已经更新了与另一个问题,请你能帮助我吗? – jmunozco

+0

你能为此发布新问题吗?这似乎是另一个问题......我会尽可能地看看它。另外,我没有在该转储中看到“image”或“text”,因此请确保提供该问题的详细信息。 –