SimplePie and get_item_tags attribute

隐身守侯 提交于 2019-12-25 06:39:33

问题


I'm using Simplepie to parse different RSS feeds, passing them to a Smarty template, and I need to return the attribute from each item line that in this example reads: NEWSX

<source url="http://whatever.url/"><![CDATA[NEWSX]]></source>

I have found the get_item_tags method will select the line and attribute having used the following:

$newssource = $item->get_item_tags('','source');

Here is my problem. I don't know how to attach each source to an item when using the following code (so that basically I can display the different source element each time alongside the usual title, link, description and so on):

$RSS = array();
foreach($items as $item){
    $feed = $item->get_feed();
    $tmp=array();

    $newssource = $item->get_item_tags('','source');
    echo $newssource[0]["data"];

    if ($feed){
        if ($enclosure = $item->get_enclosure()){
          $tmp['title'] = $item->get_title();
          $tmp['permalink'] = $item->get_permalink();
          $tmp['thumbnail'] = $enclosure->get_thumbnail();
          $tmp['description'] = $enclosure->get_description();
          $tmp['image'] = $enclosure->get_link();
        }
        $tmp['date'] = $item->get_date('j M Y');
        $tmp['content'] = $item->get_content();
        $tmp['title'] = $item->get_title();
        $tmp['link'] = $item->get_link();
        $tmp['description'] = $item->get_description();

        array_push($RSS, $tmp);
    }

}

Can it be done? Thanks in advance for any help or advice.


回答1:


So this is the solution:

$RSS = array();
foreach($items as $item){
$feed = $item->get_feed();
$tmp=array();

if ($feed){
    $tmp['date'] = $item->get_date('j M Y, g:i a');
    $tmp['content'] = $item->get_content();
    $tmp['title'] = $item->get_title();
    $tmp['link'] = $item->get_link();
    $tmp['description'] = $item->get_description();
    $tmp['source'] = $item->get_item_tags('','source')[0]["data"];

    array_push($RSS, $tmp);
    }
}

$smarty->assign( $params['assign'], $RSS );

And in the smarty template:

<div class="cont">
    <a href="{$entry.link}" target="_new">{$entry.title}</a>
    <br />
    <span class="date">Published on: <strong>{$entry.date}</strong></span><br />
   <span class="source">Via : <strong>{$entry.source}</strong></span><br />
</div>


来源:https://stackoverflow.com/questions/39574448/simplepie-and-get-item-tags-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!