0

I need to display an RSS feed on my site. I could use the built in RSS_Parser add-on, however that add-on does not appear to support adding an item enclosure (image). This is a requirement for this project.

I have elected to write my own. I have loaded the SimplePie library and I have it essentially working, just like the native Add-On, but the enclosure is not working.

I'm trying to put all the attributes into an array, like so:

        $items[] = array(
            'item_title'        => $item->get_title(),
            'item_link'         => $item->get_permalink(),
            'item_date'         => $item->get_date('U'),
            'item_content'      => $item->get_content(),
            'item_img'          =>  $item->get_enclosure(),
            'item_description'  => $item->get_description(),
            'item_categories'   => $categories,
            'item_authors'      => $authors
        );
    }
    return $items;

However, when using the item_img tag in the template, it returns a string of gibberish. Anyone know how to get it to return the url to the enclosure?

The feed i'm trying to parse is here: http://tntoday.utk.edu/feed/

I have done this with straight-up PHP, but having it as an add-on, easily accessible to content editors throughout the site would be greatly preferred.

1
  • Note: No code tags supported in comments, so I'll make it an answer after all. – Ingmar Jan 5 '16 at 5:22
1

Figured it out. Basically the error was being cause when an entry did not have an enclosure. The answer was to check if it had an enclosure first.

So it goes something like this:

if ( $enclosure = $item->get_enclosure() ) {   // If there is an image. 
        $items[] = array(
                'item_title'            => $item->get_title(),
                'item_link'             => $item->get_permalink(),
                'item_date'             => $item->get_date('U'),
                'item_content'        => $item->get_content(),
                'item_description'  => $item->get_description(),
                'item_categories'   => $categories,
                'item_authors'      => $authors,
                'item_img'          =>  $item->get_enclosure(0)->get_link()
        );
  } else {   // if there is not

            $items[] = array(
                'item_title'            => $item->get_title(),
                'item_link'             => $item->get_permalink(),
                'item_date'             => $item->get_date('U'),
                'item_content'        => $item->get_content(),
                'item_description'  => $item->get_description(),
                'item_categories'   => $categories,
                'item_authors'      => $authors,
                'item_img'      => '/images/interface/no-thumb.jpg'
            );
    }
  return $items;

0

Try a simple test like

$feed = new SimplePie();
$feed->set_feed_url('http://tntoday.utk.edu/feed/');
$feed->init();
$feed->handle_content_type();

foreach ($feed->get_items() as $item)
{
    if ($enclosure = $item->get_enclosure())
    {
        echo $enclosure->embed();
    }
}

What do you get?

2
  • To make it work I had to make some modifications... ` $feed = new SimplePie(); $feed->set_feed_url('tntoday.utk.edu/feed/'); $feed->init(); $feed->handle_content_type(); ob_start(); $this->return_data = "<h3>Hello World</h3>"; foreach ($feed->get_items() as $item) { if ($enclosure = $item->get_enclosure()) { echo $enclosure->get_link(); } } ` HOWEVER, the tag in the Template will return the "hello world"... but the simple pie stuff appears at the Beginning of the page (regardless) of the tag. – M Purdy Jan 5 '16 at 14:11
  • any thoughts on this? – M Purdy Jan 19 '16 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.