2017-02-24 58 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」,因此請確保提供該問題的詳細信息。 –