在Code Igniter中构建RSS提要


Building an RSS Feed in Code Igniter

我想构建一个RSS提要,但我的输出不是真的,如何修复它?

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

请参阅以下我的完整代码:

CI_控制器:

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

尊重MVC,在CI_Model中使用"$this->db->order_by("id","asc")->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>  

$entry->post_body未定义

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