Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中创建有效RSS提要时遇到问题_Php_Xml_Rss_Simplexml - Fatal编程技术网

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

在PHP中创建有效RSS提要时遇到问题,php,xml,rss,simplexml,Php,Xml,Rss,Simplexml,我正在尝试获取一个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='

我正在尝试获取一个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 version="2.0" ...>
还有其他方法,但这是最简单的方法。在PHP中,您只需调用函数对实体进行编码

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

”;
至于
标签问题,应该是
标记当前生成两个错误。在两个位置将其更改为
,可以修复这两个错误


否则,看起来您了解基本情况。您的
标记必须匹配。您还可以使用所谓的空标记:
,它们本身存在,但不包含内容,也不包含结束标记。

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

<rss version="2.0" ...>
还有其他方法,但这是最简单的方法。在PHP中,您只需调用函数对实体进行编码

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

”;
至于
标签问题,应该是
标记当前生成两个错误。在两个位置将其更改为
,可以修复这两个错误


否则,看起来您了解基本情况。您的
标记必须匹配。您还可以使用所谓的空标记:
,它们独立存在,但不包含内容,也不包含结束标记。

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

您的XML有很多问题;最大的问题是您正在创建一个
元素,但是原始RSS有一个
元素。这意味着元素名是
编码的
,但它位于
内容
命名空间中。这与名为
content
的元素有很大区别。我添加了注释来解释其他步骤

<?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();

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

您的XML有很多问题;最大的问题是您正在创建一个
元素,但是原始RSS有一个
元素。这意味着元素名是
编码的
,但它位于
内容
命名空间中。这与名为
content
的元素有很大区别。我添加了注释来解释其他步骤

<?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();

“不起作用”不是一个有效的问题。到底发生了什么?预期的结果是什么?你采取了哪些调试步骤(以及他们取得了哪些成果)你采取了哪些调试步骤(以及他们采取了哪些调试步骤)你采取了哪些调试步骤(以及他们采取了哪些调试步骤)你采取了哪些调试步骤?你采取了哪些调试步骤?你采取了哪些调试步骤(以及他们取得了哪些成果)??对我来说,RSS源的输出对我来说看起来很好(我不理解语言),我不理解语言),我觉得RSS源的RSS源的输出对我看起来很好(我不理解语言),但这里是我所得到的一个样本样本:以下以下是我所得到的样本:代码>代码>>>代码>>>>>>>>>>>>代码>15151515151515157555555555544444444444444444444444)))你你方方方方方方方方方方方方方(1604第四章“第四章”第四章“第五章”在该方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15在各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各نكمولكن هناك الكثير قد لم تصادفهم هذه الطريقة. الفكرة تعتمد على تحميل ملفات الالعاب الرقمية على الحاسب الشخصي …

看看吧,让你知道,RSS提要已经不再是什么东西了。“不起作用”不是一个正确的问题。到底发生了什么?预期的结果是什么?你采取了哪些调试步骤(以及他们取得了哪些成果)你采取了哪些调试步骤(以及他们采取了哪些调试步骤)你采取了哪些调试步骤(以及他们采取了哪些调试步骤)你采取了哪些调试步骤?你采取了哪些调试步骤?你采取了哪些调试步骤(以及他们取得了哪些成果)??对我来说,RSS源的输出对我来说看起来很好(我不理解语言),我不理解语言),我觉得RSS源的RSS源的输出对我看起来很好(我不理解语言),但这里是我所得到的一个样本样本:以下以下是我所得到的样本:代码>代码>>>代码>>>>>>>>>>>>代码>15151515151515157555555555544444444444444444444444)))你你方方方方方方方方方方方方方(1604第四章“第四章”第四章“第五章”在该方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15在各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各نكمولكن هناك الكثير قد لم تصادفهم هذه الطريقة. الفكرة تعتمد على تحميل ملفات الالعاب الرقمية على الحاسب الشخصي …

看看吧,让你知道,RSS提要已经不再是什么东西了。非常感谢你,米肯。你做得很好。它现在100%工作了。非常感谢米肯。你做得很好。它现在100%起作用了。