<?php
require( 'Snoopy.class.php' ); // Snoopy
require( 'simple_html_dom.php' ); // PHP Simple HTML DOM Parser
define( 'ONAIR_URL', 'https://xetown.com/board' );
define( 'RSS_LINK', 'https://xetown.com/board' );
define( 'SITE_TITLE', 'XETOWN - 자유광장' );
define( 'ENCODING', 'UTF-8' ):
define( 'LANGUAGE', 'ko' );
$lastpost = 0;
$posts = array();
$snoopy = new Snoopy;
$snoopy->read_timeout = 5;
$snoopy->timed_out = true;
$snoopy->fetch( ONAIR_URL );
$response = $snoopy->results;
$http_code = $snoopy->response_code;
unset($snoopy);
if ( strpos($http_code, '200') === FALSE ? ) die();
$html_txt = mb_convert_encoding( $response, ENCODING, 'SJIS-WIN, SJIS, EUCJP-WIN, EUCJP, JIS' );
$dom = str_get_html($html_txt);
if ($dom !== FALSE) {
$elements = (array) $dom->find('tr[class="bg2"]');
$item_count = 0;
foreach ($elements as $element) {
$post = array(
'title' => $title
, 'permalink' => $url
, 'postdate' => $postdate
, 'description' => $description
, 'tags' => $tags
);
if ( array_search( $post, $posts ) === FALSE )
$posts[] = $post;
}
unset($element); unset($elements);
}
unset($dom);
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastpost).' GMT');
header('Content-Type: text/xml; charset=' . ENCODING, true);
echo '<?xml version="1.0" encoding="' . ENCODING . '"?' .">\n";
?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" version="2.0">
<channel>
<title><?php echo SITE_TITLE; ?></title>
<link><?php echo RSS_LINK; ?></link>
<description><?php echo SITE_TITLE; ?></description>
<pubDate><?php echo date('D, d M Y H:i:s +0900', $lastpost); ?></pubDate>
<language><?php echo LANGUAGE; ?></language>
<?php foreach ( $posts as $post ) { ?>
<item>
<title><?php echo $post['title']; ?></title>
<link><?php echo $post['permalink']; ?></link>
<pubDate><?php echo date('D, d M Y H:i:s +0900', $post['postdate']); ?></pubDate>
<guid><?php echo $post['permalink']; ?></guid>
<description><![CDATA[<?php echo $post['description']; ?>]]></description>
<?php foreach ((array) $post['tags'] as $tag) { ?>
<category><![CDATA[<?php echo $tag; ?>]]></category>
<?php } ?>
</item>
<?php } ?>
</channel>
</rss>
require( 'Snoopy.class.php' ); // Snoopy
require( 'simple_html_dom.php' ); // PHP Simple HTML DOM Parser
define( 'ONAIR_URL', 'https://xetown.com/board' );
define( 'RSS_LINK', 'https://xetown.com/board' );
define( 'SITE_TITLE', 'XETOWN - 자유광장' );
define( 'ENCODING', 'UTF-8' ):
define( 'LANGUAGE', 'ko' );
$lastpost = 0;
$posts = array();
$snoopy = new Snoopy;
$snoopy->read_timeout = 5;
$snoopy->timed_out = true;
$snoopy->fetch( ONAIR_URL );
$response = $snoopy->results;
$http_code = $snoopy->response_code;
unset($snoopy);
if ( strpos($http_code, '200') === FALSE ? ) die();
$html_txt = mb_convert_encoding( $response, ENCODING, 'SJIS-WIN, SJIS, EUCJP-WIN, EUCJP, JIS' );
$dom = str_get_html($html_txt);
if ($dom !== FALSE) {
$elements = (array) $dom->find('tr[class="bg2"]');
$item_count = 0;
foreach ($elements as $element) {
$post = array(
'title' => $title
, 'permalink' => $url
, 'postdate' => $postdate
, 'description' => $description
, 'tags' => $tags
);
if ( array_search( $post, $posts ) === FALSE )
$posts[] = $post;
}
unset($element); unset($elements);
}
unset($dom);
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastpost).' GMT');
header('Content-Type: text/xml; charset=' . ENCODING, true);
echo '<?xml version="1.0" encoding="' . ENCODING . '"?' .">\n";
?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" version="2.0">
<channel>
<title><?php echo SITE_TITLE; ?></title>
<link><?php echo RSS_LINK; ?></link>
<description><?php echo SITE_TITLE; ?></description>
<pubDate><?php echo date('D, d M Y H:i:s +0900', $lastpost); ?></pubDate>
<language><?php echo LANGUAGE; ?></language>
<?php foreach ( $posts as $post ) { ?>
<item>
<title><?php echo $post['title']; ?></title>
<link><?php echo $post['permalink']; ?></link>
<pubDate><?php echo date('D, d M Y H:i:s +0900', $post['postdate']); ?></pubDate>
<guid><?php echo $post['permalink']; ?></guid>
<description><![CDATA[<?php echo $post['description']; ?>]]></description>
<?php foreach ((array) $post['tags'] as $tag) { ?>
<category><![CDATA[<?php echo $tag; ?>]]></category>
<?php } ?>
</item>
<?php } ?>
</channel>
</rss>
소스가 작동을 안합니다. 어디서 잘못되었는지 모르겠네요 .....
댓글 2
1. 작동을 안한다면 에러메시지를 확인!! (그냥 die하지 말고 무의미한 메시지 한 줄이라도 출력하도록 해보세요.)
2. 예제에 사용하신 XE타운은 원래부터 UTF-8입니다. 29줄은 일본에서나 통할 것 같네요. 지워보세요.
1. 8라인 세미콜론대신 콜론사용.
2. 27라인 FALSE뒤에 의문의 물음표.
3. 29라인 utf-8을 다시 utf-8로 convert시도하면 warning발생합니다. 필요없는 라인 입니다.
php 오류 메시지 확인하시는게 가장 빠른 해결법입니다. 오류없이 실행은 되는데 원하는 결과가 나오지 않는다면 snoopy나 simple_html_dom사용법을 다시한번 살펴보시는게 좋을꺼 같구요.