php 使用标签 <content:encoded> 检索 RSS 提要
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3346628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving RSS feed with tag <content:encoded>
提问by kylex
I have the following snippet of code:
我有以下代码片段:
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>";
}
It works EXCEPT the $entry->content
它的工作原理除了 $entry->content
That part doesn't register. In the actual feed the tag is listed as <content:encoded>but I can't get it to feed. Any suggestions?
那部分没有注册。在实际的提要中,标签被列为<content:encoded>但我无法得到它。有什么建议?
回答by Artefacto
In <content:encoded>, contentis the namespaceand encodedis the tag name.
在 中<content:encoded>,content是命名空间,encoded是标签名称。
You have to use SimpleXMLElement::children. See the output of
你必须使用SimpleXMLElement::children. 查看输出
var_dump($entry->children("content", true));
回答by Arindam Mani Das
The Tag name here is "encoded".Try this:
这里的标签名称是“编码的”。尝试这个:
$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);
}
回答by fortytwo
I'll suggest you the following code:
我会建议你使用以下代码:
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>";
}
Hope this works for you.
希望这对你有用。
回答by lukepress
.... PHP example
.... 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++;
}
// --------------------------------------------------------------------
?>
if you want to see the content HTML Source Code in your Browser, use eg:
如果您想在浏览器中查看内容 HTML 源代码,请使用例如:
print '<pre style="background:#DEDEDE">'. htmlentities($e_encoded) .'</pre>'."\n";
:=)
:=)
回答by PHPandSQL123
The working answer for this is just:
对此的工作答案只是:
$e_content = $entry->children("content", true);
$e_encoded = (string)$e_content->encoded;

