【问题标题】:SimplePie and get_item_tags attributeSimplePie 和 get_item_tags 属性
【发布时间】:2016-09-19 13:29:03
【问题描述】:

我正在使用 Simplepie 解析不同的 RSS 提要,将它们传递给 Smarty 模板,并且我需要从每个项目行返回属性,在此示例中显示为:NEWSX

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

我发现 get_item_tags 方法将选择使用以下内容的行和属性:

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

这是我的问题。使用以下代码时,我不知道如何将每个源附加到一个项目(这样基本上我每次都可以在通常的标题、链接、描述等旁边显示不同的源元素):

$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);
    }

}

可以吗?提前感谢您的任何帮助或建议。

【问题讨论】:

    标签: php rss simplepie


    【解决方案1】:

    所以这是解决方案:

    $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 );
    

    在 smarty 模板中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 2010-12-09
      • 2012-07-19
      • 2018-08-18
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      相关资源
      最近更新 更多