dousong5492 2016-06-18 15:50 采纳率: 100%
浏览 123

如何在RSS feed中获取源标记

Please, I have an RSS feed and need to get the source for every item. I'm using php and Samplepie.

for example the RSS feed is like that :

<?xml version='1.0' encoding='UTF-8'?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Landofcode.com web tutorials updates</title>
    <link>http://www.landofcode.com</link>
    <description>Web development tutorials</description>
    <item>
      <title>Wacky news story</title>
      <link>http://www.reddit.com/r/wacky</link>
      <description>Guy runs into a car and drives it!</description>
      <source url="http://www.reddit.com/.rss">
      reddit.com:what's new online!
      </source>
    </item>
    <item>
      <title>New HTML tutorials</title>
      <link>http://www.landofcode.com/html-tutorials/</link>
      <description>Five new HTML tutorials have been added</description>
    </item>
  </channel>
</rss>

and my code is like that :

 ...
   $feed->set_feed_url('http://localhost/rss/flux/rss.xml');
   $feed->init();
   $feed->handle_content_type();
    foreach ($feed->get_items() as $item){      
        echo $item->get_title();
        echo $item->get_source();
    }

but the function get_source() return NULL do you have any adea please

  • 写回答

1条回答 默认 最新

  • douyeke2695 2019-07-24 21:13
    关注

    If anybody needs the solution after three years, the problem is due the url attribute inside "source" tag

    This is not recognized by simplepie:

    <source url="http://www.reddit.com/.rss">reddit.com:what's new online!</source>
    

    This is recognized by simplepie:

    <source>reddit.com:what's new online!</source>
    

    Solution is accessing the "source" tag using the more general method "get_item_tags".

    $sourceTags = $item->get_item_tags(NULL, "source");
    $source = $sourceTags[0]['data'];
    echo $source;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害