douminfu8033
douminfu8033
2016-08-26 18:24

使用simplexml_load_file提取rss feed的完整列表

已采纳

I'm using a code to extract the items from an ebay rss feed, the only problem is that it is only extracting one item.

I suspected it was because of for each, but after searching this whole site, I couldn't find a solution. The feed URL will output 8 items (entriesPerPage=8), if you access the feed, you'll that the full xml code is there, but the parser is only getting one item.

<?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>

?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douyinjiao9351 douyinjiao9351 5年前

    Move your html inside a loop, as currently on each iteration your variables are overwritten and after the loop is over what you have is values of the last 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
    }
    
    点赞 评论 复制链接分享