1

我一直在努力让这种看似简单的代码和平工作。我正在从 wordpress 网站加载 rss,除了缩略图之外,一切都很好。由于在 XML 中它们被设置为属性而不是 nodeValue 我似乎无法导入它们。(我真的尝试了很多)

$rss = new DOMDocument();
$rss->load('http://goalprogramme.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {


    // in XML it looks like <media:thumbnail url="http://goalprogramme.files.wordpress.com/2014/01/dsc_0227.jpg?w=150"/>

    //echo $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url');

    //push items
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                'thumbnail' => $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url') // this line doesn't work !!!                
    );
    array_push($feed, $item);
}

任何帮助将不胜感激。

提前非常感谢!

4

1 回答 1

0

几小时后,我创建了另一段有效的代码。如果有人需要它,这里是:

$feed_array = array();
$feed = simplexml_load_file('http://goalprogramme.wordpress.com/feed/');

foreach ($feed->channel->item as $item) {
  $title       = (string) $item->title;
  $description = (string) $item->description;
  $link = (string) $item->link;
  $date = (string) $item->date;

  if ($media = $item->children('media', TRUE)) {
    if ($media->thumbnail) {
      $attributes = $media->thumbnail->attributes();
      $thumbnail     = (string)$attributes['url'];
    }
  }

  $item = array ( 
            'title' => $title ,
            'desc' => $description,
            'link' => $link,
            'date' => $date,
            'thumbnail' => $thumbnail                
            );
  array_push($feed_array, $item);


}
于 2014-03-05T10:09:50.197 回答