PHP로 만든 RSS 2.0 리더 소스
소스는 PHP5로 작성했고, 내장된 SAX 파서를 이용했습니다.
SAX파서는 이벤트 기반 파서라서 구현도 간단하고, 속도도 빠릅니다.
RSS는 현재 가장 널리 쓰이는 RSS 2.0을 대상으로 했습니다.
이글루스와 테터도 RSS 2.0을 지원하고 있습니다.
rss_fetch.php
lib.php
SAX파서는 이벤트 기반 파서라서 구현도 간단하고, 속도도 빠릅니다.
RSS는 현재 가장 널리 쓰이는 RSS 2.0을 대상으로 했습니다.
이글루스와 테터도 RSS 2.0을 지원하고 있습니다.
rss_fetch.php
<?php
include_once 'lib.php';
//가져올 RSS 주소를 지정하면됩니다.
$urls = array('http://sizuha.egloos.com/index.xml', 'http://kori2sal.innori.com/rss');
foreach ($urls as $url):
$handle = fopen($url, 'r');
if ($handle):
$document = '';
while (!feof($handle))
$document .= fgets($handle, 4096);
fclose($handle);
//파서 생성
$rss = new RSSParser;
//파싱
$rss->setRSS($document);
rssParse($rss);
$rss = NULL;
endif;
endforeach;
?>
include_once 'lib.php';
//가져올 RSS 주소를 지정하면됩니다.
$urls = array('http://sizuha.egloos.com/index.xml', 'http://kori2sal.innori.com/rss');
foreach ($urls as $url):
$handle = fopen($url, 'r');
if ($handle):
$document = '';
while (!feof($handle))
$document .= fgets($handle, 4096);
fclose($handle);
//파서 생성
$rss = new RSSParser;
//파싱
$rss->setRSS($document);
rssParse($rss);
$rss = NULL;
endif;
endforeach;
?>
lib.php
<?php
function rssParse($rss_obj)
{
//내장 XML 파서 생성
$xml_parser = xml_parser_create('UTF-8');
xml_set_object($xml_parser, $rss_obj);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, FALSE);
//XML 파서에 이벤트 핸들러를 할당
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
//XML 파싱
xml_parse($xml_parser, $rss_obj->getRSS());
xml_parser_free($xml_parser);
}
class RSSParser
{
private $rss_doc;
private $current_element;
private $in_item = FALSE;
private $in_description = FALSE;
private $title;
private $date;
private $link;
private $category;
private $content;
function setRSS($rss_text)
{
$this->rss_doc = $rss_text;
}
function getRSS()
{
return $this->rss_doc;
}
//태그가 시작하는 부분에서 처리할 내용
function startElement($parser, $element, $attrs)
{
if ($this->in_description) return;
$this->current_element = strtoupper($element);
switch ($this->current_element):
case 'ITEM' :
$this->in_item = TRUE;
break;
case 'DEscRIPTION' :
if ($this->in_item) {
$this->in_description = TRUE;
$this->content = '';
}
break;
default:
break;
endswitch;
}
function endElement($parser, $element)
{
$el = strtoupper($element);
if ($this->in_description and 'DEscRIPTION' != $el) return;
switch (strtoupper($el)):
case 'ITEM' :
$this->in_item = FALSE;
$this->printItem(); // 저장된 포스트를 출력하거나 DB로 자장하면 됨.
break;
case 'DEscRIPTION' :
if ($this->in_item) {
$this->in_description = FALSE;
}
break;
default:
break;
endswitch;
$this->current_element = '';
}
function characterData($parser, $data)
{
if ('' == trim($data)) return;
if ($this->in_item):
switch ($this->current_element):
case 'TITLE' :
$this->title = $data;
break;
case 'DEscRIPTION' :
$this->content .= $data; //반드시 .= 연산자를 써야함!
break;
case 'CATEGORY' :
$this->category = $data;
break;
case 'PUBDATE' :
$this->date = $data;
break;
case 'LINK' :
$this->link = $data;
break;
endswitch;
endif;
}
//여기서는 바로 출력을 하지만, DB에 저장하는 방식으로 구현할 수도 있습니다.
private function printItem()
{
echo "<P><STRONG>";
echo $this->title;
echo "</STRONG>";
echo " (";
echo $this->date.")</P>";
echo $this->content;
echo "<BR>";
echo $this->category." | ";
echo $this->link;
echo "<br><br>";
}
}//end of class
?>
function rssParse($rss_obj)
{
//내장 XML 파서 생성
$xml_parser = xml_parser_create('UTF-8');
xml_set_object($xml_parser, $rss_obj);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, FALSE);
//XML 파서에 이벤트 핸들러를 할당
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
//XML 파싱
xml_parse($xml_parser, $rss_obj->getRSS());
xml_parser_free($xml_parser);
}
class RSSParser
{
private $rss_doc;
private $current_element;
private $in_item = FALSE;
private $in_description = FALSE;
private $title;
private $date;
private $link;
private $category;
private $content;
function setRSS($rss_text)
{
$this->rss_doc = $rss_text;
}
function getRSS()
{
return $this->rss_doc;
}
//태그가 시작하는 부분에서 처리할 내용
function startElement($parser, $element, $attrs)
{
if ($this->in_description) return;
$this->current_element = strtoupper($element);
switch ($this->current_element):
case 'ITEM' :
$this->in_item = TRUE;
break;
case 'DEscRIPTION' :
if ($this->in_item) {
$this->in_description = TRUE;
$this->content = '';
}
break;
default:
break;
endswitch;
}
function endElement($parser, $element)
{
$el = strtoupper($element);
if ($this->in_description and 'DEscRIPTION' != $el) return;
switch (strtoupper($el)):
case 'ITEM' :
$this->in_item = FALSE;
$this->printItem(); // 저장된 포스트를 출력하거나 DB로 자장하면 됨.
break;
case 'DEscRIPTION' :
if ($this->in_item) {
$this->in_description = FALSE;
}
break;
default:
break;
endswitch;
$this->current_element = '';
}
function characterData($parser, $data)
{
if ('' == trim($data)) return;
if ($this->in_item):
switch ($this->current_element):
case 'TITLE' :
$this->title = $data;
break;
case 'DEscRIPTION' :
$this->content .= $data; //반드시 .= 연산자를 써야함!
break;
case 'CATEGORY' :
$this->category = $data;
break;
case 'PUBDATE' :
$this->date = $data;
break;
case 'LINK' :
$this->link = $data;
break;
endswitch;
endif;
}
//여기서는 바로 출력을 하지만, DB에 저장하는 방식으로 구현할 수도 있습니다.
private function printItem()
{
echo "<P><STRONG>";
echo $this->title;
echo "</STRONG>";
echo " (";
echo $this->date.")</P>";
echo $this->content;
echo "<BR>";
echo $this->category." | ";
echo $this->link;
echo "<br><br>";
}
}//end of class
?>
RSS 리더를 만들 때의 주의사항
1. RSS는 UTF-8로 인코딩되어 있습니다.
따라서 RSS의 데이터를 변환없이 그냥 웹페이지에 뿌리려면, 웹페이지 역시 UTF-8로 인코딩되어 있어야 합니다. 저는 모든 작업
파일들(PHP, HTML, TXT)을 전부 UTF-8로 통일했습니다.
2. RSS의 PubDate 태그는 게시물의 날짜를 담고 있는
태그이지만, 문제는 이것이 GMT 시간입니다. 웹에 게시할때는 현지(한국) 시간으로 변환해야 할 것입니다.
댓글 3개
12년 전
감솨합니다!
devdev
12년 전
편하겠네요..
dsv421
12년 전
감사합니다
게시글 목록
| 번호 | 제목 |
|---|---|
| 10762 |
jQuery
회사에 히스토리에 써먹을 만한 자료
8
|
| 10754 | |
| 10752 |
MySQL
고수님들 도와주세요!
1
|
| 10749 |
Mobile
json 파싱방법
2
|
| 10746 | |
| 10740 | |
| 10738 | |
| 10736 | |
| 10733 |
JavaScript
주민번호, 외국인번호 검사 스크립트
2
|
| 10731 |
Mobile
Base64.java 파일입니다.
1
|
| 10730 | |
| 10727 | |
| 10725 |
MySQL
Mysql Text필드
1
|
| 10721 | |
| 10719 |
Mobile
[안드로이드] 프리퍼런스 사용 방법
1
|
| 10718 | |
| 10712 | |
| 10711 |
Mobile
[ios]사진촬영후 이미지 편집 샘플입니다.
|
| 10710 |
JavaScript
Html , javascript , css 강좌 사이트 입니다.
|
| 10709 |
JavaScript
주민번호 자바스크립트 체크
|
| 10707 |
jQuery
안녕하세요 jquery에 대해 물어볼게있습니다
1
|
| 10706 | |
| 20108 | |
| 10705 |
Mobile
[안드로이드] 자신의 폰에 깔려있는 앱들 검색하기
|
| 10703 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기