在Code Igniter中构建RSS Feed

时间:2012-03-13 21:10:59

标签: php xml codeigniter rss

我想构建一个RSS Feed,但我的输出不正确,如何解决?

我使用本教程:http://www.derekallard.com/blog/post/building-an-rss-feed-in-code-igniter/

请参阅以下完整代码:

是CI_Controller:

function rss_feed($limit = NULL)  
{
    $data['encoding'] = 'utf-8';
    $data['feed_name'] = 'neginph.com';
    $data['feed_url'] = 'http://www.neginph.com';
    $data['page_description'] = 'Mono-calcium phosphate and calcium phosphate producer in the Persian month Dey';
    $data['page_language'] = 'en-en';
    $data['creator_email'] = 'Negin phosphate North';
    $data['posts'] = $this->db->order_by("id", "asc")->get_where('rss_feed', array('id' => 1));;    
    header("Content-Type: application/rss+xml");
    $this->load->view('rss_feed', $data);

}

查看:

<?php 
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.codeigniter.com/" />

    <?php foreach($posts->result() as $entry): ?>

        <item>

          <title><?php echo xml_convert($entry->post_title); ?></title>
          <link><?php echo site_url('blog/post/' . $entry->url_title) ?></link>
          <guid><?php echo site_url('blog/post/' . $entry->url_title) ?></guid>

          <description><![CDATA[
      <?= str_replace('/img/post_resources/', base_url() . 'img/post_resources/', $entry->post_body); ?>
      ]]></description>
      <pubDate><?php echo date ('r', $entry->post_date);?></pubDate>
        </item>


    <?php endforeach; ?>

    </channel></rss> 

这是我在url中的输出,为什么这个显示的输出代码是这样的? :http://www.neginph.com/site/rss_feed

2 个答案:

答案 0 :(得分:2)

尊重MVC,在CI_Model中使用“$ this-&gt; db-&gt; order_by(”id“,”asc“) - &gt; get_where ...”,例如:

你的CI_Controller:

function rss_feed()  
    {  
            $data['encoding']           = 'utf-8'; // the encoding
            $data['feed_name']          = 'MyWebsite.com'; // your website  
            $data['feed_url']           = 'http://www.MyWebsite.com/feed'; // the url to your feed  
            $data['page_description']   = 'What my site is about comes here'; // some description  
            $data['page_language']      = 'en-en'; // the language  
            $data['creator_email']      = 'mail@me.com'; // your email  
            $data['posts']              = $this->feed_model->getRecentPosts();  
            header("Content-Type: application/rss+xml"); // important!
            $this->load->view('rss', $data);

    }
你在CI_Model中的

class Feed_Model extends CI_Model {

    // get all postings  
    function getRecentPosts()  
    {  
        $this->db->order_by('id', 'asc');
        $this->db->limit(10);
        return $this->db->get('posts');
    }

}

你喂:

<?php 
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.codeigniter.com/" />

        <?php foreach($posts->result() as $entry): ?>  

            <item>  
                <title><?php echo xml_convert($entry->title); ?></title> 
                <link><?php echo site_url('blog/post/' . $entry->id) ?></link>
                <guid><?php echo site_url('blog/post/' . $entry->id) ?></guid>

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

            </item>  

        <?php endforeach; ?>

        </admin:generatoragent>

    </channel>

</rss>  

答案 1 :(得分:0)

$entry->post_body未定义

也是一件小事,但你有两个分号:$data['posts'] = $this->db->order_by("id", "asc")->get_where('rss_feed', array('id' => 1));;这就是烦我。有效是的,但我不喜欢它!