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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?