【问题标题】:Pulling Images from rss/atom feeds using magpie rss使用 magpie rss 从 rss/atom 提要中提取图像
【发布时间】:2010-08-16 05:13:15
【问题描述】:

我正在使用 php 和 magpie,并且想要一种检测提要项中图像的通用方法。我知道有些网站将图片放在附件标签中,有些则像 images[rss] 这样,有些则只是将其添加到描述中。有没有通用的检测rss项目是否有图片并在magpie解析后提取图片url的功能?

我认为从描述中提取需要正则表达式,但我不擅长这些。如果可以的话,请帮忙。

【问题讨论】:

    标签: image rss extract magpie


    【解决方案1】:

    我自己花了很长时间寻找一种通过 Magpie 在 RSS 中显示图像的方法,最后我不得不检查代码以弄清楚如何让它工作。

    就像你说的,Magpie 没有在元素中提取图像的原因是因为它们是使用“enclosure”标签指定的,这是一个空标签,其中信息位于属性中,例如

    <enclosure url="http://www.mysite.com/myphoto.jpg" length="14478" type="image/jpeg" />
    

    为了让它快速为我工作,我在 rss_parse.inc 中添加了以下代码行:

        function feed_start_element($p, $element, &$attrs) {
       ...
       if ( $el == 'channel' )
       {
          $this->inchannel = true;
       }
       ...
    
       // START EDIT - add this elseif condition to the if ($el=xxx) statement.
       // Checks if element is enclosure tag, and if so store the attribute values
       elseif ($el == 'enclosure' ) {
          if ( isset($attrs['url']) ) {
             $this->current_item['enclosure_url'] = $attrs['url'];
             $this->current_item['enclosure_type'] = $attrs['type'];
             $this->current_item['enclosure_length'] = $attrs['length'];
          }
       }
       // END EDIT
       ...
    }
    

    图片的 url 在 $myRSSitem['enclosure_url'] 中,大小在 $myRSSitem['enclosure_length'] 中。 请注意,附件标签可以引用多种类型的媒体,因此首先通过检查 $myRSSitem['enclosure_type'] 来检查类型是否实际上是图像。

    也许其他人有更好的建议,我相信这可以更优雅地从其他空标签中获取属性,但我需要一个快速修复(截止日期压力),但我希望这可以帮助其他有困难的人!

    【讨论】:

    • 这个解决方案非常适合我自己的混乱情况。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多