php - 使用 SimpleXML 读取 RSS 提要时出现问题

标签 php xml codeigniter rss

我正在尝试阅读一个简单的 Twitpic rss 提要,但运气不佳。我看不出我的代码有什么问题,但它只在使用 print_r() 时返回以下内容

Array ( [title] => SimpleXMLElement Object ( ) )

这是我的代码:

function get_twitpics() {

    /* get raw feed */

    $url = 'http://www.twitpic.com/photos/Shealan/feed.rss'; 
    $raw = file_get_contents($url); 
    $xml = new SimpleXmlElement($raw);

    /* create array from feed items */

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

        $article = array();
        $article['title'] = $item->description;
    }

    return $article;
}

最佳答案

foreach($xml->channel->item as $item) {
    $article = array(); // so you want to erase the contents of $article with each iteration, eh?
    $article['title'] = $item->description;
}

如果您不仅仅对最后一个元素感兴趣,您可能需要查看您的 for 循环 - 即

$article = array();
foreach($xml->channel->item as $item) {
    $article[]['title'] = $item->description;
}

关于php - 使用 SimpleXML 读取 RSS 提要时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3908590/

相关文章:

win64 中的 PHP APC "emulated"

php - Symfony 缓存 :clear timeout - Exceeded 300 seconds

php - 如何使用 mysql 对最近的数据进行排序以及计数

java - 如何使用jaxb获取anyType值?

php - 循环嵌套菜单

jquery - 从 PHP、MySQL 生成的 XML 填充 JPlayer 播放列表

html - 如何在 Highcharts 中将数据标签设置为垂直

codeigniter - 包罗万象的 Controller /路由

php - Codeigniter 连接多个 id 并显示到 View

php - Codeigniter CSRF - 它是如何工作的