【问题标题】:Building an RSS Feed in Code Igniter在 Codeigniter 中构建 RSS 提要
【发布时间】:2012-03-13 21:10:58
【问题描述】:

我想建立一个 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

【问题讨论】:

    标签: php xml codeigniter rss


    【解决方案1】:

    尊重 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>  
    

    【讨论】:

      【解决方案2】:

      $entry-&gt;post_body 未定义

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

      【讨论】:

      • $entry-&gt;post_body应该用在哪里?
      • 嗯,它似乎没有从您的数据库中选择。
      猜你喜欢
      • 2011-05-19
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多