Codeigniter

RSS  및 xml 파일 만드는 방법으로 코드이그나이터를 활용해 보겠습니다.

RSS 피드로 정의 된 컨텐트가 변경 될 때마다 피드를 통해 업데이트를 얻을 수 있습니다. 예를 들어 블로깅 사이트는 "전체 사이트 피드", "댓글 전용 피드", "라벨 별 사이트 피드", "개별 게시물 코멘트 피드"등의 유형을 제공 할 수 있습니다.

이 튜토리얼에서는 CodeIgniter Framework로 RSS 피드를 빌드 할 것입니다.

ci 3.0 버전

그런 다음, feed.php라는 Controller 폴더 (applications / controllers / feed.php)에 CodeIgniter 컨트롤러 (클래스)를 만듭니다.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Feed extends CI_Controller {
 
    public function __construct(){  
        parent::__construct();
        //load xml helper
        $this->load->helper(array('xml'));
    }
 
    public function index()
    {
       // set feed Name will display at title area and page top
       $this->data['feed_name'] = 'phpis.com';
       // set page encoding
       $this->data['encoding'] = 'utf-8';
       // set feed url
       $this->data['feed_url'] = 'http://phpis.com/feed';
       // set page language 
       $this->data['page_language'] = 'en';
       // set page Description
       $this->data['page_description'] = 'PHP | CodeIgniter | Wordpress | MySQL | Css3 | HTML5 | JQuery';
       // set author email 
       $this->data['creator_email'] = 'arjunphp@gmail.com';
       // this line is very important, this will let browser to display XML format output
       header("Content-Type: application/rss+xml");
 
       $this->load->view('feed_view',$this->data);
 
    }
}
 
/*End of file feed.php*/
/*Location .application/controllers/feed.php*/

 

각 줄마다 줄을 훑어 보았습니다.

그런 다음 뷰 파일을 생성하고 컨트롤러 / 뷰에있는 views 폴더로 이동 한 후 
feed_view.php 라는 파일을 만들고,


<?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/" />
<!-- repeat this block for more items --> 
<item>
<title>replace with your data</title>
<link>
replace with your data
</link>
<guid>replace with your data</guid>
<description> replace with your data </description>
<pubDate>replace with your data</pubDate>
</item> 
<!-- end item Block -->
</channel>
</rss>

 

 

동적 RSS 피드

매우 간단한 코딩으로 RSS 피드를 동적으로 쉽게 생성 할 수 있습니다.

모델 작성 controller / model /에 rss_model.php가 호출되었습니다.

 

<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');
 
class Feed_model extends  CI_Model{
  
    function __construct()
    {
        parent:: __construct();
        
    }
    // get all
    function get_feeds()
    {
        $sql="select * from ci_board order by board_id desc";
        $query = $this->db->query($sql);
        
        return $query->result();
  
        if ($query->numRows() > 0) { // check for rows
              return $query->result();
        }
        return;
    }
    
}
 

 

이 모델을 다음과 같이 컨트롤러에로드하십시오.

 $this->load->model('feed_model');

 

그런 다음 컨트롤러에서 모델 메서드를 호출하고 뷰에 전달하고 전체 컨트롤러를 참조하십시오.

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Feed extends CI_Controller {
 
    public function __construct(){  
        parent::__construct();
        //xml 로더
        $this->load->helper(array('xml'));
        // 기타 로더
        $this->load->helper(array('explode'));
        $this->load->model('feed_model');
    }
 
    public function index()
    {
       // 제목
       $this->data['feed_name'] = 'macaronics.net';
       
       $this->data['encoding'] = 'utf8';
       // feed url 설정
       $this->data['feed_url'] = 'http://macaronics.net/index.php/feed';
       // 언어설정 
       $this->data['page_language'] = 'KR';
       // 페이지 설명
       $this->data['page_description'] = 'Java | Spring | JSP | PHP |CodeIgniter | Wordpress | MySQL | Css3 | HTML5 | JQuery 
       개발 방법 | 블로그 포스팅 | 코딩 방법 | 모바일 | 업체홍보 | 커뮤니티 | 쇼핑몰 홈페이지 | 반응형 | 웹사이트 제작 | 전문업체
        안드로이드 | 하이브리드앱 | 리눅스 | 개발자 Q&A | 개발자 고충상담 | 정기모임/스터디 | 사는얘기 | 학원및 기타홍보 
         좋은회사 | 나쁜회사 | 구인/구직 | 벼룩시장 | 교육 | 한국영화 | 외국영화 | 애니메이션 | 드라마 | 실시TV | 게임커뮤니티 | 만화커뮤니티';
       // 제작자 이메일
       $this->data['creator_email'] = 'braverokmc79@gmail.com';
       // get the feeds  from database through model method called get_feeds()
       
       $this->data['query'] = $this->feed_model->get_feeds();
  
       // this line is very important, this will let browser to display XML format output  
       header("Content-Type: application/rss+xml; charset=utf-8");
       
       $this->load->view('feed_view', $this->data);
 
    }
}
 

 

마지막 단계는 뷰 파일을 변경하고, 뷰 파일 인 feed_view.php를 엽니 다.

<?php echo '<?xml version="1.0" encoding="utf-8"?>';?>

<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 iconv($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/" />
<!-- repeat this block for more items -->
<?php if(isset($query) && is_array($query)): 
foreach($query as $row):?>
<item>
<title><?php echo htmlspecialchars($row->subject); ?></title>
<link><?php echo 'http://macaronics.net/index.php/'.url_name($row->board_subject).'/'.$row->board_subject.'/view/'.$row->board_id; ?></link>
<guid><?php echo $row->board_id; ?></guid>
<description><?php echo htmlspecialchars($row->contents); ?></description>
<pubDate><?php echo $row->reg_date; ?></pubDate>
</item> 
<?php endforeach; 
endif;?>
<!-- end item Block -->
</channel>
</rss>


 

 

결과 =>

http://macaronics.net/index.php/feed

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

남자란 이기심의 목적이외의 것에는 마음을 쓰지 않는다. - S. 모옴

댓글 ( 4)

댓글 남기기

작성