0

所以我正在使用这个功能:

function getFeed($feed_url) {  
    $content = file_get_contents($feed_url);  
    $x = new SimpleXmlElement($content);  
    echo "<ul>";  
    foreach($x->channel->item as $entry) {  
        echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
        echo "<li>Category:" . $entry->category . "</li>";
    }  
    echo "</ul>";  
} 

我正在使用的 RSS 提要有多个“类别”值。我如何检索所有这些?

如下图所示

<category>Cat 1</category>
<category>Cat 2</category>
4

2 回答 2

0

你有没有尝试过:

function getFeed($feed_url) {  
    $content = file_get_contents($feed_url);  
    $x = new SimpleXmlElement($content);  
    echo "<ul>";  
    foreach($x->channel->item as $entry) {  
        echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
        foreach($entry->category as $writeMe)
            echo "<li>Category:" . $writeMe . "</li>";
    }  
    echo "</ul>";  
}
于 2012-10-17T15:18:59.587 回答
0

你可以改变:

echo "<li>Category:" . $entry->category . "</li>";

和:

foreach($entry->category as $category) {
    echo "<li>Category:" . $category . "</li>";
}

但是你能提供 XML 结构吗?它应该是:

<categories>
    <category>Cat 1</category>
    <category>Cat 2</category> 
</categories> 
于 2012-10-17T15:20:58.823 回答