dongmeng2687 2019-05-22 02:56 采纳率: 0%
浏览 120
已采纳

无法在PHP中创建有效的RSS源

I'm trying to get an RSS feed, change some text, and then serve it again as an RSS feed. However, the code I've written doesn't validate properly. I get these errors:

line 3, column 0: Missing rss attribute: version

line 14, column 6: Undefined item element: content (10 occurrences)

Here is my code:

<?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>";
  • 写回答

2条回答 默认 最新

  • dongzhong1891 2019-05-22 03:53
    关注

    Just as you don't treat XML as a string when parsing it, you don't treat it as as string when you create it. Use the proper tools to create your XML; in this case, the DomDocument class.

    You had a number of problems with your XML; biggest is that you were creating a <content> element, but the original RSS had a <content:encoded> element. That means the element name is encoded but it's in the content namespace. Big difference between that and an element named content. I've added comments to explain the other steps.

    <?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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序