php rss feed show images

蓝咒 提交于 2019-12-11 04:27:46

问题


I am trying to show the images in the following rss feed and would appreciate help from somebody please. I have tried getAttribute but am not sure how to format it or where in the code to put it, so I have taken it out to avoid confusion. The following code is working, but I need to add the code to display the images.

<?php

$rss = new DOMDocument();
$rss->load('http://xml.thinkspain.com/think-spain-feeds/spanish-news.xml');

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array ( 
 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
 );
array_push($feed, $item);
} 

$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}  

?>

回答1:


You can get attribute by using getAttribute :

$node->getElementsByTagName('enclosure')->item(0)->getAttribute('url')


来源:https://stackoverflow.com/questions/20939039/php-rss-feed-show-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!