 |
|
|  |
php·Î ÀÛ¼ºÇÑ °£´Ü RSS2.0 ¸®´õ |
|
|
 |
13³â Àü |
¼Ò½º´Â PHP5·Î ÀÛ¼ºÇß°í, ³»ÀåµÈ SAX Æļ¸¦ ÀÌ¿ëÇß½À´Ï´Ù.
SAXÆļ´Â À̺¥Æ® ±â¹Ý Æļ¶ó¼ ±¸Çöµµ °£´ÜÇÏ°í, ¼Óµµµµ ºü¸¨´Ï´Ù.
RSS´Â ÇöÀç °¡Àå ³Î¸® ¾²ÀÌ´Â RSS 2.0À» ´ë»óÀ¸·Î Çß½À´Ï´Ù.
À̱۷罺¿Í Å×Å͵µ RSS 2.0À» Áö¿øÇÏ°í ÀÖ½À´Ï´Ù.
¿Ï¼ºµÈ ¼Ò½º ÀÚü´Â °£´ÜÇѵ¥, Á¦°¡ PHP¸¦ óÀ½ ´Ù·ïºÁ¼ ½ÃÇàÂø¿À¸¦ ¸¹ÀÌ °ÅÃƽÀ´Ï´Ù. »ç½Ç Áö±Ýµµ PHPÀÇ ¹®¹ý°ú ¶óÀ̺귯¸®¸¦ ¿ÏÀüÈ÷ ¼÷ÁöÇÑ »óÅ°¡ ¾Æ´Ï¶ó¼, ¼Ò½º ÀÚüµµ ±×¸® ±ò²ûÇÏ´Ù°í´Â ¸» ¸øÇÏ°Ú½À´Ï´Ù.
¿©±â °ø°³µÈ ¼Ò½º´Â Á¦°¡ ÀÛ¾÷ÇÑ °ÍÀ» Àû´çÈ÷ °£Ã߸° °ÍÀÔ´Ï´Ù.
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;
?>
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
?>
RSS ¸®´õ¸¦ ¸¸µé ¶§ÀÇ ÁÖÀÇ»çÇ×
1. RSS´Â UTF-8·Î ÀÎÄÚµùµÇ¾î ÀÖ½À´Ï´Ù. µû¶ó¼ RSSÀÇ µ¥ÀÌÅ͸¦ º¯È¯¾øÀÌ ±×³É À¥ÆäÀÌÁö¿¡ »Ñ¸®·Á¸é, À¥ÆäÀÌÁö ¿ª½Ã UTF-8·Î ÀÎÄÚµùµÇ¾î ÀÖ¾î¾ß ÇÕ´Ï´Ù. Àú´Â ¸ðµç ÀÛ¾÷ ÆÄÀϵé(PHP, HTML, TXT)À» ÀüºÎ UTF-8·Î ÅëÀÏÇß½À´Ï´Ù.
2. RSSÀÇ PubDate ű״ °Ô½Ã¹°ÀÇ ³¯Â¥¸¦ ´ã°í ÀÖ´Â Å×±×ÀÌÁö¸¸, ¹®Á¦´Â ÀÌ°ÍÀÌ GMT ½Ã°£ÀÔ´Ï´Ù. À¥¿¡ °Ô½ÃÇÒ¶§´Â ÇöÁö(Çѱ¹) ½Ã°£À¸·Î º¯È¯ÇØ¾ß ÇÒ °ÍÀÔ´Ï´Ù.
¡Ø Ãß°¡ »çÇ×
Á¦°¡ Âü°íÇÑ °ÍÀº À̱۷罺¿Í Å×ÅÍÀÇ RSSÀÔ´Ï´Ù.
»ç½Ç RSSÀÇ ÀÎÄÚµù ¹æ½Ä°ú Å×±× ºÎºÐ¿¡¼´Â Æ÷Å»¸¶´Ù Â÷ÀÌ°¡ ÀÖ½À´Ï´Ù.
|
|
̵̧ : 655 |
̵̧
¸ñ·Ï
|
|
|  |
|