开发者

Pulling Images from rss/atom feeds using magpie rss

Im using php and magpie and would like a general way of detecting images in feed item. I know some websites place images within the enclosure tag, others like this images[rss] and some simply add it to description. Is there any one with a general function for detecting if rss item has image and extracting image url after its been parsed by magpie?

i t开发者_开发技巧hink reqular expressions would be needed to extract from description but im a noob at those. Please help if you can.


I spent ages searching for a way of displaying images in RSS via Magpie myself, and in the end I had to examine the code to figure out how to get it to work.

Like you say, the reason Magpie doesn't pick up images in the element is because they are specified using the 'enclosure' tag, which is an empty tag where the information is in the attributes, e.g.

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

As a hack to get it to work quickly for me I added the following lines of code into 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
   ...
}

The url to the image is in $myRSSitem['enclosure_url'] and the size is in $myRSSitem['enclosure_length']. Note that enclosure tags can refer to many types of media, so first check if the type is actually an image by checking $myRSSitem['enclosure_type'].

Maybe someone else has a better suggestion and I'm sure this could be done more elegantly to pick up attributes from other empty tags, but I needed a v quick fix (deadline pressures) but I hope this might help someone else in difficulty!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜