J

RSS Reader 만들기

여기에 오래전에 RSS 관련해서 글을 적었을 때 제가 로컬에서 테스트해서 올렸는데
최근에 웹호스팅에서 하다보니 보안상 fopen은 막아 놓아더라구요.

그래서 사이트 검색 해보니 fsoketopen을 사용해서 하는 방식이 있어 다시 정래해서 올립니다.

우선 사용하는 페이지입니다.
RSS 기본 자체가 utf-8 형식이니 이점만 주의하시면 될 것 같아요.
요즘 메타사이트 관련 작업 문의가 많은데..
개발자님들 참고하셔서 커스터마이징 해서 사용하시기 바랍니다.

------------------------------------------------------------------------------------------
<html>
<head>
<title>Test</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
<?

//echo phpinfo();

include_once './lib/lib.php';

//가져올 RSS 주소를 지정하면됩니다.
// 새로운 적용 방식 - 보안 때문에 fopen을 사용할 수가 없군요
$urls = array('http://blog.rss.naver.com/minsoub.xml', 'http://kori2sal.innori.com/rss');


foreach ($urls as $url):

$handle = get_url_fsockopen($url);

if ($handle != "")
{
$rss = new RSSParser;

// parsing
$rss->setRSS($handle);
rssParse($rss);

$rss = NULL;
}
endforeach;

// 이전 소스 적용 방식
/* foreach ($urls as $url):
$handle = fopen($url, 'r');

if ($handle):
$document = '';

while (!feof($handle))
$document .= fgets($handle, 4096);

fclose($handle);

echo document;

//파서 생성
$rss = new RSSParser;

//파싱
$rss->setRSS($document);
rssParse($rss);

$rss = NULL;
endif;
endforeach; */

?>
</body>
</html>
------------------------------------------------------------------------------------------

다음은 클래스와 함수입니다.
-------------------------------------------------------------------------------------------
<?

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);
}


function get_url_fsockopen( $url ) {
$URL_parsed = parse_url($url);

$host = $URL_parsed["host"];
$port = $URL_parsed["port"];
if ($port==0)
$port = 80;

$path = $URL_parsed["path"];
if ($URL_parsed["query"] != "")
$path .= "?".$URL_parsed["query"];

$out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";

$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs($fp, $out);
$body = false;
while (!feof($fp)) {
$s = fgets($fp, 128);
if ( $body )
$in .= $s;
if ( $s == "\r\n" )
$body = true;
}

fclose($fp);
return $in;
}
}


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

?><div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>
|

댓글 5개

RSS리더 필요하던 참이였는데 잘사용하겠습니다 ~ 감사합니다
궁금한게 있습니다.. 확장 rss라고 <content:encoded>를 사용하는 rss가 있는데 이것은 어떻게 구현해야하나요.
잘됩니다. 감사합니다!
감사합니다^^
감사합니다 ㅠ.ㅠ fopen막힌거 찾느라고 정말 고생했는데 ㅠ.ㅠ
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
11-10-25 조회 2,464
11-10-18 조회 3,989
11-10-18 조회 2,359
11-10-15 조회 3,569
11-10-10 조회 2,636
11-10-02 조회 2,561
11-09-28 조회 2,376
11-09-24 조회 2,459
11-09-22 조회 2,362
11-09-05 조회 2,844
11-08-31 조회 2,213
11-08-31 조회 2,517
11-08-31 조회 2,376
11-08-29 조회 2,941
11-08-28 조회 2,418
11-08-25 조회 2,478
11-08-17 조회 2,203
11-08-16 조회 2,423
11-08-11 조회 2,889
11-08-07 조회 2,589
11-08-03 조회 2,308
11-07-22 조회 2,056
11-07-12 조회 3,271
11-06-27 조회 2,065
11-06-01 조회 1,733
11-05-17 조회 2,512
11-05-01 조회 2,585
11-04-28 조회 2,874
11-04-28 조회 2,915
11-04-13 조회 2,575
11-03-27 조회 2,702
11-03-27 조회 2,580
11-03-14 조회 1,995
11-03-02 조회 2,311
11-03-02 조회 2,549
11-02-27 조회 2,981
11-02-23 조회 2,357
11-02-19 조회 2,499
11-02-14 조회 4,120
11-02-03 조회 2,911
11-01-24 조회 3,854
11-01-04 조회 2,381
10-12-08 조회 5,386
10-11-15 조회 2,382
10-11-14 조회 2,565
10-11-14 조회 3,588
10-10-29 조회 3,668
10-10-19 조회 2,677
10-10-07 조회 2,509
10-10-05 조회 4,413
10-09-18 조회 2,716
10-09-18 조회 2,468
10-09-17 조회 4,854
10-09-15 조회 2,802
10-09-15 조회 2,329
10-09-15 조회 2,801
10-09-15 조회 2,645
10-09-11 조회 3,971
10-09-11 조회 2,479
10-09-11 조회 2,514
10-09-07 조회 3,790
10-09-07 조회 3,330
10-08-29 조회 2,783
10-07-01 조회 3,041
10-06-30 조회 2,441
10-06-30 조회 2,627
10-06-30 조회 2,979
10-06-13 조회 2,004
10-04-15 조회 3,175
10-04-04 조회 3,407
10-02-23 조회 3,499
10-02-09 조회 3,120
10-01-25 조회 2,617
10-01-25 조회 2,718
09-12-27 조회 4,368
09-11-23 조회 3,585
09-11-20 조회 4,824
09-08-30 조회 3,020
09-08-30 조회 3,019
09-08-12 조회 2,969
09-07-22 조회 4,045
09-07-22 조회 4,691
09-07-15 조회 5,867
09-07-11 조회 2,942
09-06-17 조회 2,685
09-06-15 조회 2,972
09-05-29 조회 3,240
09-05-11 조회 5,881
09-05-08 조회 2,631
09-04-30 조회 3,757
09-04-17 조회 3,068
09-04-04 조회 3,253
09-03-05 조회 3,638
09-02-09 조회 3,611
09-02-03 조회 6,110
08-12-27 조회 3,180
08-12-20 조회 4,275
08-11-21 조회 5,234
08-11-14 조회 2,807
08-11-03 조회 3,257