0

我无法将 RSS 用作有效的提要。这是 rss: http: //mimjob.com/news/rss

PHP:

<?php ob_start();  echo'<?xml version="1.0" encoding="utf-8" ?>' . "\n"; ?>
<rss version="2.0"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:content="http://purl.org/rss/1.0/modules/content/">  

    <channel>

        <title><?php echo $feed_name; ?> </title> 
        <link><?php echo $feed_url; ?> </link> 
        <description><?php echo $page_description; ?></description>  
        <dc:language><?php echo $page_language; ?></dc:language>  
        <dc:creator><?php echo $creator_email; ?></dc:creator>
        <dc:rights>Copyright <?php echo gmdate("Y", time()); ?></dc:rights>  

        <admin:generatorAgent rdf:resource="http://www.mzksh.com/" />

        <?php foreach($news->result() as $n): ?>  

            <item>  
                <title><?php echo xml_convert($n->title); ?></title> 
                <link><?php echo base_url('news/get/' . $n->id) ?></link>
                <guid><?php echo base_url('news/get/' . $n->id) ?></guid>

                <description><![CDATA[<?php echo character_limiter($n->text, 200); ?>]]></description>
                <pubDate><?php echo  $n->date;?></pubDate>

            </item>  

        <?php endforeach; ?>

        </admin:generatoragent>

    </channel>

</rss>
<?php
$output = ob_get_contents();
ob_end_clean();
echo $output;
?>

我已添加ob_start以删除空白,但仍无法使其正常工作。我还检查了之前是否有任何空格<?php

我正在使用 codeigniter 框架。

4

2 回答 2

3

那是因为 CodeIgniter 通过其OutputClass 处理所有输出。

首先ob_start从视图文件中删除该部分,然后通过这种方式更改当前Content-type和输出:

class Home extends CI_Controller
{
    public function rss()
    {
        $data = $this->load->view('your_rss_view_file', '', TRUE);

        $this->output
            ->set_content_type('application/rss+xml') // This is the standard MIME type
            ->set_output($data); // set the output
    }
}
于 2013-08-09T08:56:25.127 回答
0

您只需要在向浏览器回显某些内容之前放置 xml 标头即可。例子:

class homepage extends CI_Controller{

    function index(){
        header("Content-type: text/xml; charset=utf-8");

        $this->load->view('my_view_file'); // this is your code from the example
    }
}
于 2013-08-09T05:50:04.550 回答