2012-11-16 50 views
2

我用这从一个RSS feed解析内容:获取内容:从RSS编码喂

$rss = new DOMDocument(); 
$rss->load($feedurl); 
$feed = array(); 
foreach ($rss->getElementsByTagName('item') as $node) { 
    $item = array(
     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
     'content' => $node->getElementsByTagName('content:encoded') 
      ->item(0)->nodeValue, 
     'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 
     'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, 
     'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue, 
    ); 
    array_push($feed, $item); 
} 
$limit = 1; 
for ($x = 0; $x < $limit; $x++) { 
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']); 
    $link = $feed[$x]['link']; 
    $content = $feed[$x]['content']; 
    $guid = $feed[$x]['guid']; 
} 

和分配内容的变量。

他们都在工作,除了那个。为什么是这样?

有人请帮我解决这个问题:)

+2

您使用'内容:encoded'在你的XML标记的名字吗? '内容:'这里可能被视为命名空间。检查[getElementxByTagName](http://www.php.net/manual/en/domelement.getelementsbytagnamens.php),看看它是否有帮助。 – Passerby

+1

http://stackoverflow.com/questions/3346628/retrieving-rss-feed-with-tag-contentencoded – fortytwo

回答

3

有一个类似的问题,我接着fortytwo 提到的链接,发现this answer因为它显示(将提交一份有关这个问题的答案编辑)这是不工作的,尽管只进行了一些小修改,但我能够按照需要进行工作。这使得它非常简单。

<?php 
$xml=("https://en.blog.wordpress.com/feed/"); 


function getFeed($feed_url) { 
     $feeds = file_get_contents($feed_url); 
     $feeds = str_replace("<content:encoded>","<contentEncoded>",$feeds); 
     $feeds = str_replace("</content:encoded>","</contentEncoded>",$feeds); 
     $rss = simplexml_load_string($feeds); 

    echo "<ul>"; 
    $x=$rss; 
     foreach($x->channel->item as $entry) { 
      echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>"; 
      echo "<li>$entry->contentEncoded</li>"; 
     } 
    echo "</ul>"; 
} 

getFeed($xml);