我正在尝试制作一个 rss 阅读器 / 提取器 / 解析器,或者任何你想叫它的东西。提要网址是http://www.jutarnji.hr/rss
我已经创建了一个 php 函数,除了从 rss 项目中获取图像之外,其他所有功能都可以工作,因为它是一个非常复杂的 rss 提要,唯一简单的方法是使用 xpath 抛出一个数组,我不知道通过在内部自动提取该数组foreach 循环显示每个提要项旁边的图像注意:这不是我的 rss 提要,我正在尝试获取其他人的 rss 提要:
function parser($feedURL) {
$rss = simplexml_load_file($feedURL);
$rss->registerXPathNamespace('n', 'http://jutarnji.hr/rss');
$i = 0;
foreach ($rss->entry as $feedItem) {
$i++;
$myDate = ($feedItem->updated);
$dateForm = explode(" ", $myDate);
if (date("j.n.Y.", strtotime($myDate)) == date("j.n.Y.")) {
$niceDate = date("H:i", strtotime($myDate));
}
else if (date("j.n.Y.", strtotime($myDate)) == date("j.n.Y.", strtotime("yesterday"))) {
$niceDate = "jučer u " . date("H:i", strtotime($myDate));
}
else {
$niceDate = date("j.n.Y.", strtotime($myDate)) . " u " . date("H:i", strtotime($myDate));
}
$feedurl = ($feedItem->link->attributes()->href);
$imgUrl = $rss->xpath("//n:link[@rel='enclosure']/@href"); //This throws an array which I cannot extract and use in this foreach loop as well
$urltoimage = (string)$imgUrl[0][0][1]; // This was my try to atleast get a specific item but not working
echo "<div class='box'>
<h3><a target='_blank' href='$feedurl' title='$feedItem->title'>" . $feedItem->title . "</a></h3>
<p><img src='$urltomage'/>" . $feedItem->description . "</p>
<p class='belowpost'><img src='images/time.png'/>" . $niceDate. "<a class='cont' target='_blank' href='$feedurl' title='$feedItem->title'>Više</a></p></div>";
if($i >= 20) break;
} }
问题出在某处:
$imgUrl = $rss->xpath("//n:link[@rel='enclosure']/@href"); //This throws an array which I cannot extract and use in this foreach loop as well
$urltoimage = (string)$imgUrl[0][0][1]; // This was my try to atleast get a specific item but not working
只要不包含任何书籍,任何帮助都将不胜感激,大声笑提前谢谢,这对我来说真的很重要