【问题标题】:Extract full list of rss feed using simplexml_load_file使用 simplexml_load_file 提取 rss 提要的完整列表
【发布时间】:2016-08-26 18:24:26
【问题描述】:

我正在使用代码从 ebay rss 提要中提取项目,唯一的问题是它只提取一个项目。

我怀疑是因为for each,但是在搜索了整个站点之后,我找不到解决方案。提要 URL 将输出 8 项 (entriesPerPage=8),如果您访问提要,您会发现完整的 xml 代码在那里,但解析器只获取一项。

<?php

$feedurl = "http://rest.ebay.com/epn/v1/find/item.rss?keyword=%28jewelry%2Ccraft%2Cclothing%2Cshoes%2Cdiy%29&sortOrder=BestMatch&programid=1&campaignid=5337945426&toolid=10039&listingType1=All&lgeo=1&topRatedSeller=true&hideDuplicateItems=true&entriesPerPage=8&feedType=rss";

$rss = simplexml_load_file($feedurl);

foreach ($rss->channel->item as $item) {

$link = $item->link;

$title = $item->title;

$description = $item->description;

}

?>
<div class="mainproductebayfloatright-bottom">
<div class="aroundebay">
<?
print "<div class=\"titleebay\">" . $title . "</div>";
print $description;
?>
</div>
</div>

?>

【问题讨论】:

  • 可能是因为您正在循环浏览一项? $rss->频道->项目
  • 但是既然是foreach,它不应该得到页面上的每个项目,而不是一个?

标签: php xml rss


【解决方案1】:

将您的 html 移动到循环中,因为当前在每次迭代中,您的变量都被覆盖,并且在循环结束后,您所拥有的是最后一个 xml-item 的值:

foreach ($rss->channel->item as $item) {
    $link = $item->link;
    $title = $item->title;
    $description = $item->description;?>
    <div class="mainproductebayfloatright-bottom">
        <div class="aroundebay">
    <?php
        // simple title
        print "<div class=\"titleebay\">" . $title . "</div>";
        // title-link
        print "<a href=\"" . $link . "\">" . $title . "</div>";
        print $description;
    ?>
        </div>
    </div>
<?php
}

【讨论】:

  • 您的代码返回一个空白页,我什至激活了错误报告,但它根本没有显示错误。另外,为什么最后有一个带括号的开放php标签?提前谢谢
  • 使用&lt;?php 而不是&lt;?open php tag with a parenthesis in the end 关闭一个 foreach 循环。
  • 还是不行。你运行的是什么版本的php?
  • 检查返回的 xml,确实有 -&gt;item 字段。所以返回-&gt;item。编辑了我的答案。
  • 当,它解决了这个问题。我现在欠你一杯啤酒! :D
猜你喜欢
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 2012-03-04
  • 1970-01-01
相关资源
最近更新 更多