Search code examples
phpforeachrsssimplepie

SimplePie: Call to undefined method SimplePie_Item::get_image_url()


I have a foreach loop like this:

$SimplePie->set_feed_url(array('links'));
$SimplePie->init();
$SimplePie->handle_content_type();
foreach ($SimplePie->get_items() as $item) {
    echo $item->get_image_url();
}

But I always get this error Call to undefined method SimplePie_Item::get_image_url(). The same thing is if I try any else method which is starting with get_image... I printed out whole $item variable and I found that inside it there is url for image, but I don't know what is wrong that I can't get url with this method.

What am I doing wrong?


Solution

  • After a little of hacking SimplePie library I came across that it doesn't have support for posts's images yet. It's really easy to get images out of posts, with already created function get_item_tags() which allows you to browse SimplePie's parsed output. So here is the code for getting first image of all if it exists:

    $image = $item->get_item_tags('', 'image')[0]['child']['']['url'][0]['data']; // manually getting the first image's url
    if (isset($image)) // if exists
    {
        echo $image; // do something with it
    }
    

    If you want to get more images, just put $item->get_item_tags('', 'image') in foreach and get out the links.

    I hope that SimplePie authors will implement function for this tag as soon as possible :)