phprsssimplepie

Getting image url from RSS feed using simplepie


I am very new to php and simplepie. I would like to be able to use simplepie to store an image url to a variable. For my example, I will use the ebay rss feed (http://deals.ebay.com/feeds/rss). The image that I am trying to get the url of is in a <image src= tag. When i use the code

foreach ($feed->get_items() as $item):
?>
<?php echo $item->get_description(); ?>
<?php endforeach; ?>

The image and description are shown, but I am unable to store the image url to a variable. How can I use simplepie to store an image url to a variable?

Thanks


Solution

  • You can use DOM parser such as SimpleHTML like:

    require_once('simple_html_dom.php');
    
    foreach ($feed->get_items() as $item)
    {
     $description =  $item->get_description();
     $desc_dom = str_get_html($description);
     $image = $desc_dom->find('img', 0);
     $image_url = $image->src;
    }
    

    It returns the URL of first image. If you want to get all images, you can store them in array like $desc_dom->find('img');

    If you are using SimpleHTML on composer projects, use

    composer require mgargano/simplehtmldom