在 PHP 中创建有效的 RSS 提要时遇到问题

我正在尝试获取 RSS 提要,更改一些文本,然后将其作为 RSS 提要再次提供。但是,我编写的代码没有正确验证。我收到这些错误:


第 3 行,第 0 列:缺少 rss 属性:版本


第 14 行,第 6 列:未定义的项目元素:内容(出现 10 次)


这是我的代码:


<?php

header("Content-type: text/xml");


echo "<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet type='text/xsl'?>

<?xml-stylesheet type='text/xsl' media='screen'                 

href='/~d/styles/rss2full.xsl'?>

<rss xmlns:content='http://purl.org/rss/1.0/modules/content/'>


<channel>

<title>Blaakdeer</title>

<description>Blog RSS</description>

<language>en-us</language>

";



$html = "";

$url = "http://feeds.feedburner.com/vga4a/mPSm";

$xml = simplexml_load_file($url);


for ($i = 0; $i < 10; $i++){

$title = $xml->channel->item[$i]->title;

$description = $xml->channel->item[$i]->description;

$content = $xml->channel->item[$i]->children("content", true);

$content = preg_replace("/The post.*/","", $content);


echo "<item>

<title>$title</title>

<description>$description</description>

<content>$content</content>

</item>";

 }



echo "</channel></rss>";


森林海
浏览 127回答 2
2回答

临摹微笑

第一个错误只是缺少一个属性,很简单:<rss&nbsp;version="2.0"&nbsp;...>对于<p>和其他 HTML 元素,您需要对它们进行转义。该文件应如下所示:&lt;p&gt;...还有其他方法,但这是最简单的方法。在 PHP 中,您可以调用一个函数来对实体进行编码。$output&nbsp;.=&nbsp;htmlspecialchars("&nbsp;<p>Paragraph</p>&nbsp;");至于<content>标签问题,应该是<description>。该<content>标签当前生成两个错误。<description>在两个地方都将其更改为应该可以解决两个错误。否则,您似乎了解基础知识。你<open>和</close>标签和那些必须匹配。您还可以使用所谓的空标签:<empty/>它们独立存在,但不包含内容且没有结束标签。
打开App,查看更多内容
随时随地看视频慕课网APP