使用simplepie从RSS feed获取图像URL

aci*_*dic 5 php rss simplepie

我是php和simplepie的新手.我希望能够使用simplepie将图像URL存储到变量中.对于我的例子,我将使用ebay rss feed(http://deals.ebay.com/feeds/rss).我想要获取网址的图片位于<image src=标记中.当我使用代码

foreach ($feed->get_items() as $item):
?>
<?php echo $item->get_description(); ?>
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)

显示图像和说明,但我无法将图像URL存储到变量中.如何使用simplepie将图像URL存储到变量?

谢谢

Val*_*yev 7

你可以使用像SimpleHTML这样的DOM解析器:

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;
}
Run Code Online (Sandbox Code Playgroud)

它返回第一个图像的URL.如果你想获得所有图像,你可以将它们存储在数组中 $desc_dom->find('img');

如果您在作曲家项目中使用SimpleHTML,请使用

composer require mgargano/simplehtmldom
Run Code Online (Sandbox Code Playgroud)