简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM