0

我这样做并且它有效。

<?php
    function load_file($url) 
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $xml = simplexml_load_string(curl_exec($ch));
        return $xml;
    }

    $feedurl = 'http://www.astrology.com/horoscopes/daily-extended.rss';
    $rss = load_file($feedurl);

    $items = array();
    $count = 0;
    foreach ($rss->channel->item->description as $i => $description) 
    {
        $items[$count++] = $description;
    }
    echo $items[0];
?>

当我echo $items[1]; 没有显示下一个时。不知道我做错了什么。

4

1 回答 1

4

这是您的xml的示例:

<channel>
    <item>
        <description>blah</description>
    </item>
    <item>
        <description>blah1</description>
    </item>
    <item>
        <description>blah2</description>
    </item>
    <item>
        <description>blah3</description>
    </item>
</channel>

当您这样做时,$rss->channel->item->description您将获得第item一个description.

您需要先遍历items然后获取每个描述。

例如:

$descriptions = array();
foreach($rss->channel->item as $item){
    $descriptions[] = $item->description;
    // note I don't need the $count variable... if you just use
    // [] then it auto increments the array count for you.
}

希望有帮助。它未经测试,但应该可以工作。

于 2012-09-07T08:34:57.120 回答