我有以下代码片段:
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>$entry->content</li>";
echo "</ul>";
}
除了$entry->content
该部分未注册。在实际Feed中,代码列为<content:encoded>
,但我无法将其输入。有什么建议吗?
答案 0 :(得分:38)
在<content:encoded>
中,content
是namespace,encoded
是标记名称。
您必须使用SimpleXMLElement::children
。参见
var_dump($entry->children("content", true));
答案 1 :(得分:35)
此处的标记名称为“已编码”。 试试这个:
$url = 'put_your_feed_URL';
$rss = new DOMDocument();
$rss->load($url);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'content' => $node->getElementsByTagName('encoded')->item(0)->nodeValue
);
array_push($feed, $item);
}
答案 2 :(得分:8)
我建议您使用以下代码:
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>";
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>";
}
希望这适合你。
答案 3 :(得分:3)
.... PHP示例
<?php
// --------------------------------------------------------------------
$feed_url = 'http://www.tagesschau.de/xml/rss2';
$xml_data = simplexml_load_file($feed_url);
// --------------------------------------------------------------------
$i=0;
foreach($xml_data->channel->item as $ritem) {
// --------------------------------------
$e_title = (string)$ritem->title;
$e_link = (string)$ritem->link;
$e_pubDate = (string)$ritem->pubDate;
$e_description = (string)$ritem->description;
$e_guid = (string)$ritem->guid;
$e_content = $ritem->children("content", true);
$e_encoded = (string)$e_content->encoded;
$n = ($i+1);
// --------------------------------------
print '<p> ---------- '. $n .' ---------- </p>'."\n";
print "\n";
print '<div class="entry" style="margin:0 auto; padding:4px; text-align:left;">'."\n";
print '<p> Title: '. $e_title .'</p>'."\n";
print '<p> Link: '. $e_link .'</p>'."\n";
print '<p> Date: '. $e_pubDate .'</p>'."\n";
print '<p> Desc: '. $e_description .'</p>'."\n";
print '<p> Guid: '. $e_guid .'</p>'."\n";
print '<p> Content: </p>'."\n";
print '<p style="background:#DEDEDE">'. $e_encoded .'</p>'."\n";
print '</div>'."\n";
// --------------------------------------
print '<br />'."\n";
print '<br />'."\n";
$i++;
}
// --------------------------------------------------------------------
?>
如果您想在浏览器中查看内容HTML源代码,请使用例如:
print '<pre style="background:#DEDEDE">'. htmlentities($e_encoded) .'</pre>'."\n";
:=)
答案 4 :(得分:1)
对此的有效答案是:
$e_content = $entry->children("content", true);
$e_encoded = (string)$e_content->encoded;