【问题标题】:Trouble creating a valid RSS feed in PHP在 PHP 中创建有效的 RSS 提要时遇到问题
【发布时间】:2019-05-22 02:56:08
【问题描述】:

我正在尝试获取 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>";

【问题讨论】:

  • “没用”不是一个有效的问题。到底发生了什么?预期的结果是什么?您采取了哪些调试步骤(以及它们产生了什么)?
  • RSS 提要的输出对我来说看起来不错(我不懂语言),但这是我得到的示例:&lt;item&gt; &lt;title&gt;اليكم الطريقة الاسرع لتحميل العاب البلايستيشن 4 الرقمية من المتجر “الستور”&lt;/title&gt; &lt;description&gt;&lt;p&gt;الكثير منا يعاني من بطئ في عملية تحميل العاب البلايستيشن 4 رغم ان سرعة الانترنت الخاصة بشبكة المنزل يكون معدلها اعلى من ذلك بكثير و هنا في هذا التقرير سأقدم لكم طريقة قد يعرفها البعض منكم و لكن هناك الكثير قد لم تصادفهم هذه الطريقة. الفكرة تعتمد على تحميل ملفات الالعاب الرقمية على الحاسب الشخصي &amp;#8230;&lt;/p&gt;
  • 您知道,RSS 提要不再是什么东西了。

标签: php xml rss simplexml


【解决方案1】:

正如您在解析 XML 时不将其视为字符串一样,在创建 XML 时也不会将其视为字符串。使用适当的工具来创建您的 XML;在本例中为 DomDocument 类。

您的 XML 存在许多问题;最大的问题是您正在创建一个 &lt;content&gt; 元素,但原始 RSS 有一个 &lt;content:encoded&gt; 元素。这意味着元素名称是encoded,但它位于content 命名空间中。这与名为content 的元素之间存在很大差异。我添加了 cmets 来解释其他步骤。

<?php

// create the XML document with version and encoding
$xml = new DomDocument("1.0", "UTF-8");
$xml->formatOutput = true;
// add the stylesheet PI
$xml->appendChild(
    $xml->createProcessingInstruction(
        'xml-stylesheet',
        'type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"'
    )
);
// create the root element
$root = $xml->appendChild($xml->createElement('rss'));
// add the version attribute
$v = $root->appendChild($xml->createAttribute('version'));
$v->appendChild($xml->createTextNode('2.0'));
// add the namespace
$root->setAttributeNS(
    'http://www.w3.org/2000/xmlns/',
    'xmlns:content',
    'http://purl.org/rss/1.0/modules/content/'
);
// create some child elements
$ch = $root->appendChild($xml->createElement('channel'));
// specify the text directly as second argument to
// createElement because it doesn't need escaping
$ch->appendChild($xml->createElement('title', 'Blaakdeer'));
$ch->appendChild($xml->createElement('description', 'Blog RSS'));
$ch->appendChild($xml->createElement('language', 'en-us'));

$url = "http://feeds.feedburner.com/vga4a/mPSm";
$rss = simplexml_load_file($url);

for ($i = 0; $i < 10; $i++) {
    if (empty($rss->channel->item[$i])) {
        continue;
    }
    $title = $rss->channel->item[$i]->title;
    $description = $rss->channel->item[$i]->description;
    $content = $rss->channel->item[$i]->children("content", true);
    $content = preg_replace("/The post.*/","", $content);

    $item_el = $ch->appendChild($xml->createElement('item'));

    $title_el = $item_el->appendChild($xml->createElement('title'));
    // this stuff is unknown so it has to be escaped
    // so have to create a separate text node
    $title_el->appendChild($xml->createTextNode($title));

    $desc_el = $item_el->appendChild($xml->createElement('description'));
    // the other alternative is to create a cdata section
    $desc_el->appendChild($xml->createCDataSection($description));

    // the content:encoded element is not the same as a content element
    // the element must be created with the proper namespace prefix
    $cont_el = $item_el->appendChild(
        $xml->createElementNS(
            'http://purl.org/rss/1.0/modules/content/',
            'content:encoded'
        )
    );
    $cont_el->appendChild($xml->createCDataSection($content));
}

header("Content-type: text/xml");
echo $xml->saveXML();

【讨论】:

  • 非常感谢迈肯。你做得很好。它现在 100% 正常工作。
【解决方案2】:

第一个错误只是缺少属性,很简单:

<rss version="2.0" ...>

对于&lt;p&gt; 和其他HTML 元素,您需要对它们进行转义。该文件应如下所示:

&lt;p&gt;...

还有其他方法,但这是最简单的方法。在 PHP 中,您可以调用一个函数来对实体进行编码。

$output .= htmlspecialchars(" <p>Paragraph</p> ");

至于&lt;content&gt;标签问题,应该是&lt;description&gt;&lt;content&gt; 标签当前会产生两个错误。在这两个地方将其更改为 &lt;description&gt; 应该可以修复这两个错误。

否则,您似乎了解基础知识。您的&lt;open&gt;&lt;/close&gt; 标签必须匹配。您还可以使用所谓的空标签:&lt;empty/&gt;,它们独立存在,但不包含内容且没有结束标签。

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多