CodeIgniter RSS输出作为有效的feed

时间:2013-08-09 00:44:58

标签: php codeigniter rss

我无法将RSS作为有效的Feed使用。这是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框架。

2 个答案:

答案 0 :(得分:3)

这是因为CodeIgniter通过其Output类来处理所有输出。

首先从视图文件中删除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
    }
}

答案 1 :(得分: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
    }
}