news_rss.php
// 가져온 뉴스 중 몇개까지 보여줄건지..
define("PAGE_LIMIT", 100);
// 날짜 형태
define("DATE_FORMAT", "Y/m/d H:i:s");
// 리프레시 시간 (분)
define("REFRESH_MINUTE", 2);
$lastmodified = get_last_modified_time();
// 리프레시 시간 내에 있으면 cache hit
if(time() - $lastmodified< REFRESH_MINUTE * 60 && file_exists("currentnews.html")) {
$contents = trim(file("currentnews.html"));
echo $contents;
} else {
require_once "RSS.php";
// url_fopen 허용
if (ini_get("allow_url_fopen") == 0) {
ini_set("allow_url_fopen", 1);
}
$news = array();
$channel_list = array();
// 채널 목록을 읽어온다.
$fd = fopen ("newschannel.txt", "r");
while (!feof ($fd)) {
$channel_list[] = trim(fgets($fd, 4096));
}
fclose ($fd);
// 각 채널에서 뉴스를 읽어온다.
foreach($channel_list as $rss_url) {
if(PEAR::isError($r =& new XML_RSS($rss_url)))
continue;
$r->parse();
/*
keys :title, link, description, dc:date (or pubdate)
*/
$channel_info = array();
foreach($r->getChannelInfo() as $key => $val) {
$channel_info["cp_" . $key] = $val;
}
foreach ($r->getItems() as $value) {
array_change_key_case($value, CASE_LOWER);
$tmp_arr = array_merge($value, $channel_info);
if($tmp_arr["dc:date"])
$tmp_arr[date] = $tmp_arr["dc:date"];
else if($tmp_arr["pubdate"])
$tmp_arr[date] = $tmp_arr["pubdate"];
// unix timestamp를 키로 한다.
$key = strtotime($tmp_arr[date]);
$tmp_arr[date] = date(DATE_FORMAT, $key);
// 키가 중복되지 않도록....
while(array_key_exists($key,$news)) {
$key--;
}
if($tmp_arr[description])
$tmp_arr[description] = parse_description($tmp_arr[description]);
$news[$key] = $tmp_arr;
}
}
// 시간의 역순으로 정렬
krsort($news, SORT_NUMERIC);
$k = 0;
ob_start();
// 뉴스 아이템을 화면에 출력한다.
foreach($news as $item) {
$k++;
// 제한 갯수 까지만 보여준다
if($k > PAGE_LIMIT)
break;
?>
= $item[title] ?> (= $item[date] ?>) [= $item[cp_title] ?>]
}
$contents = ob_get_contents();
ob_end_flush();
unset($news);
set_modified($contents);
}
// description 교정
function parse_description($body) {
$current = array(
"$lastmodifiedtime = trim(file("newslastmodifiedtime.txt"));
}
return $lastmodifiedtime;
}
// 수정 내역 반영
function set_modified($contents) {
$fd = fopen ("newslastmodifiedtime.txt", "w");
fwrite($fd, time());
fclose ($fd);
$fd = fopen ("currentnews.html", "w");
fwrite($fd, $contents);
fclose ($fd);
}
?>
define("PAGE_LIMIT", 100);
// 날짜 형태
define("DATE_FORMAT", "Y/m/d H:i:s");
// 리프레시 시간 (분)
define("REFRESH_MINUTE", 2);
$lastmodified = get_last_modified_time();
// 리프레시 시간 내에 있으면 cache hit
if(time() - $lastmodified< REFRESH_MINUTE * 60 && file_exists("currentnews.html")) {
$contents = trim(file("currentnews.html"));
echo $contents;
} else {
require_once "RSS.php";
// url_fopen 허용
if (ini_get("allow_url_fopen") == 0) {
ini_set("allow_url_fopen", 1);
}
$news = array();
$channel_list = array();
// 채널 목록을 읽어온다.
$fd = fopen ("newschannel.txt", "r");
while (!feof ($fd)) {
$channel_list[] = trim(fgets($fd, 4096));
}
fclose ($fd);
// 각 채널에서 뉴스를 읽어온다.
foreach($channel_list as $rss_url) {
if(PEAR::isError($r =& new XML_RSS($rss_url)))
continue;
$r->parse();
/*
keys :title, link, description, dc:date (or pubdate)
*/
$channel_info = array();
foreach($r->getChannelInfo() as $key => $val) {
$channel_info["cp_" . $key] = $val;
}
foreach ($r->getItems() as $value) {
array_change_key_case($value, CASE_LOWER);
$tmp_arr = array_merge($value, $channel_info);
if($tmp_arr["dc:date"])
$tmp_arr[date] = $tmp_arr["dc:date"];
else if($tmp_arr["pubdate"])
$tmp_arr[date] = $tmp_arr["pubdate"];
// unix timestamp를 키로 한다.
$key = strtotime($tmp_arr[date]);
$tmp_arr[date] = date(DATE_FORMAT, $key);
// 키가 중복되지 않도록....
while(array_key_exists($key,$news)) {
$key--;
}
if($tmp_arr[description])
$tmp_arr[description] = parse_description($tmp_arr[description]);
$news[$key] = $tmp_arr;
}
}
// 시간의 역순으로 정렬
krsort($news, SORT_NUMERIC);
$k = 0;
ob_start();
// 뉴스 아이템을 화면에 출력한다.
foreach($news as $item) {
$k++;
// 제한 갯수 까지만 보여준다
if($k > PAGE_LIMIT)
break;
?>
= $item[description] ?>
}
$contents = ob_get_contents();
ob_end_flush();
unset($news);
set_modified($contents);
}
// description 교정
function parse_description($body) {
$current = array(
"$lastmodifiedtime = trim(file("newslastmodifiedtime.txt"));
}
return $lastmodifiedtime;
}
// 수정 내역 반영
function set_modified($contents) {
$fd = fopen ("newslastmodifiedtime.txt", "w");
fwrite($fd, time());
fclose ($fd);
$fd = fopen ("currentnews.html", "w");
fwrite($fd, $contents);
fclose ($fd);
}
?>
newschannel.txt 내용 :
http://www.chosun.com/rss/rss.xmlhttp://rss.joins.com/joins_news_list.xmlhttp://rss.joins.com/joins_ilgan_list.xmlhttp://kr.rss.news.yahoo.com/Yahoo_nocut1001.xmlhttp://media.ohmynews.com/rss/ohmynews.xmlhttp://rss.news.yahoo.com/rss/topstorieshttp://www.zdnet.co.kr/services/rss/rss2.0.htmhttp://wired.daum.net/rss.xml
http://pear.php.net/package/XML_RSS
'download'클릭하면 0.9.2라고 된 압축파일 다운.
압축풀어서 다른거는 다 버리고 그냥 RSS.php 파일만
원하는 위치에 놓으시고요~ (/home/RSS.php)
newschannel.txt 을 저장 (/home/newschannel.txt)
news_rss.php을 저장 (/home/news_rss.php)
토탈3개의파일 :
/home/RSS.php
/home/newschannel.txt
/home/news_rss.php
여기서 news_rss.php 파일을 여셔서..
16번째줄, 26번째줄 에 있는 파일 경로를 수정
[RSS.php 파일 경로 - 수정전]
require_once "RSS.php";
[수정후]
require_once $DOCUMENT_ROOT."/RSS.php";
[RSS.php 파일 경로 - 수정전]
$fd = fopen ($"newschannel.txt", "r");
[수정후]
[RSS.php 파일 경로 - 수정전]
$fd = fopen ($DOCUMENT_ROOT."/newschannel.txt", "r");
그리고는 주소창에서 news_rss.php 를 불러옴
'download'클릭하면 0.9.2라고 된 압축파일 다운.
압축풀어서 다른거는 다 버리고 그냥 RSS.php 파일만
원하는 위치에 놓으시고요~ (/home/RSS.php)
newschannel.txt 을 저장 (/home/newschannel.txt)
news_rss.php을 저장 (/home/news_rss.php)
토탈3개의파일 :
/home/RSS.php
/home/newschannel.txt
/home/news_rss.php
여기서 news_rss.php 파일을 여셔서..
16번째줄, 26번째줄 에 있는 파일 경로를 수정
[RSS.php 파일 경로 - 수정전]
require_once "RSS.php";
[수정후]
require_once $DOCUMENT_ROOT."/RSS.php";
[RSS.php 파일 경로 - 수정전]
$fd = fopen ($"newschannel.txt", "r");
[수정후]
[RSS.php 파일 경로 - 수정전]
$fd = fopen ($DOCUMENT_ROOT."/newschannel.txt", "r");
그리고는 주소창에서 news_rss.php 를 불러옴
출처 : fguy.com
========================================================
PHP5로 RSS구현하기
소스1 (rss.reader.class.php)
/**
* http://web.resource.org/rss/1.0/spec
* http://blogs.law.harvard.edu
*/
class rss
{
private $parser = null;
private $current_tag = null;
private $current_attribute = null;
private $rdf_code = null;
private $item_count = 0;
public $channel = array();
public function __construct()
{
$this->parser = xml_parser_create();
}
public function parse($rss_file)
{
xml_set_object($this->parser, &$this);
xml_set_element_handler($this->parser, "_startElement", "_endElement");
xml_set_character_data_handler($this->parser, "_characterData");
$fp = @fopen($rss_file, "r");
if(!$fp)
{
throw new Exception("Error reading RSS file : " . $rss_file);
}
else
{
while($rssData = fread($fp, 4096))
{
if(xml_parse($this->parser, $rssData, feof($fp)) == false)
{
throw new Exception(xml_error_string(xml_get_error_code($this->parser)) . " Line : " . xml_get_current_line_number($this->parser));
}
}
fclose($fp);
xml_parser_free($this->parser);
}
}
private function _startElement($parser, $name, $attribute = null)
{
$this->current_tag = $name;
$this->current_attribute = $attribute;
switch($this->current_tag)
{
case "CHANNEL" :
$this->rdf_code = "channel";
break;
case "IMAGE" :
$this->rdf_code = "image";
break;
case "ITEM" :
$this->rdf_code = "item";
break;
case "CLOUD" :
$this->rdf_code = "cloud";
break;
case "TTL" :
$this->rdf_code = "ttl";
break;
case "TEXTINPUT" :
$this->rdf_code = "textinput";
break;
}
}
private function _endElement($parser, $name, $attribute = null)
{
if($name == "ITEM")
{
$this->item_count += 1;
}
$this->current_tag = null;
$this->current_attribute = null;
}
private function _characterData($parser, $cdata)
{
$cdata = iconv(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING), "EUC-KR", $cdata);
if($this->rdf_code == "channel")
{
switch($this->current_tag)
{
case "TITLE" :
$this->channel['channel']['title'] .= $cdata;
break;
case "LINK" :
$this->channel['channel']['link'] .= $cdata;
break;
case "DESCRIPTION" :
$this->channel['channel']['description'] .= $cdata;
break;
case "LANGUAGE" :
$this->channel['channel']['language'] .= $cdata;
break;
case "COPYRIGHT" :
$this->channel['channel']['copyright'] .= $cdata;
break;
case "MANAGINGEDITOR" :
$this->channel['channel']['managingeditor'] .= $cdata;
break;
case "WEBMASTER" :
$this->channel['channel']['webmaster'] .= $cdata;
break;
case "PUBDATE" :
$this->channel['channel']['pubdate'] .= $cdata;
break;
case "LASTBUILDDATE" :
$this->channel['channel']['lastbuilddate'] .= $cdata;
break;
case "DOCS" :
$this->channel['channel']['docs'] .= $cdata;
break;
}
}
else if($this->rdf_code == "image")
{
switch($this->current_tag)
{
case "URL" :
$this->channel['channel']['image_url'] .= $cdata;
break;
case "WIDTH" :
$this->channel['channel']['image_width'] .= $cdata;
break;
case "HEIGHT" :
$this->channel['channel']['image_height'] .= $cdata;
break;
case "TITLE" :
$this->channel['channel']['image_title'] .= $cdata;
break;
case "LINK" :
$this->channel['channel']['image_link'] .= $cdata;
break;
}
}
else if($this->rdf_code == "item")
{
switch($this->current_tag)
{
case "LINK" :
$this->channel['item'][$this->item_count]['link'] .= $cdata;
break;
case "TITLE" :
$this->channel['item'][$this->item_count]['title'] .= $cdata;
break;
case "DESCRIPTION" :
$this->channel['item'][$this->item_count]['description'] .= $cdata;
break;
case "PUBDATE" :
$this->channel['item'][$this->item_count]['pubdate'] .= $cdata;
break;
}
}
}
public function __destruct()
{
}
};
?>
-------------------------------------------------
소스2 (rss.html)
include_once("rss.reader.class.php");
/** using("xml");
**/
$rss = new rss();
try
{
// rss 주소
$rss->parse("http://slashdot.org/rss/index.rss");
}
catch(Exception $e)
{
die($e->getMessage());
}
?>
New Document
/**
* http://web.resource.org/rss/1.0/spec
* http://blogs.law.harvard.edu
*/
class rss
{
private $parser = null;
private $current_tag = null;
private $current_attribute = null;
private $rdf_code = null;
private $item_count = 0;
public $channel = array();
public function __construct()
{
$this->parser = xml_parser_create();
}
public function parse($rss_file)
{
xml_set_object($this->parser, &$this);
xml_set_element_handler($this->parser, "_startElement", "_endElement");
xml_set_character_data_handler($this->parser, "_characterData");
$fp = @fopen($rss_file, "r");
if(!$fp)
{
throw new Exception("Error reading RSS file : " . $rss_file);
}
else
{
while($rssData = fread($fp, 4096))
{
if(xml_parse($this->parser, $rssData, feof($fp)) == false)
{
throw new Exception(xml_error_string(xml_get_error_code($this->parser)) . " Line : " . xml_get_current_line_number($this->parser));
}
}
fclose($fp);
xml_parser_free($this->parser);
}
}
private function _startElement($parser, $name, $attribute = null)
{
$this->current_tag = $name;
$this->current_attribute = $attribute;
switch($this->current_tag)
{
case "CHANNEL" :
$this->rdf_code = "channel";
break;
case "IMAGE" :
$this->rdf_code = "image";
break;
case "ITEM" :
$this->rdf_code = "item";
break;
case "CLOUD" :
$this->rdf_code = "cloud";
break;
case "TTL" :
$this->rdf_code = "ttl";
break;
case "TEXTINPUT" :
$this->rdf_code = "textinput";
break;
}
}
private function _endElement($parser, $name, $attribute = null)
{
if($name == "ITEM")
{
$this->item_count += 1;
}
$this->current_tag = null;
$this->current_attribute = null;
}
private function _characterData($parser, $cdata)
{
$cdata = iconv(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING), "EUC-KR", $cdata);
if($this->rdf_code == "channel")
{
switch($this->current_tag)
{
case "TITLE" :
$this->channel['channel']['title'] .= $cdata;
break;
case "LINK" :
$this->channel['channel']['link'] .= $cdata;
break;
case "DESCRIPTION" :
$this->channel['channel']['description'] .= $cdata;
break;
case "LANGUAGE" :
$this->channel['channel']['language'] .= $cdata;
break;
case "COPYRIGHT" :
$this->channel['channel']['copyright'] .= $cdata;
break;
case "MANAGINGEDITOR" :
$this->channel['channel']['managingeditor'] .= $cdata;
break;
case "WEBMASTER" :
$this->channel['channel']['webmaster'] .= $cdata;
break;
case "PUBDATE" :
$this->channel['channel']['pubdate'] .= $cdata;
break;
case "LASTBUILDDATE" :
$this->channel['channel']['lastbuilddate'] .= $cdata;
break;
case "DOCS" :
$this->channel['channel']['docs'] .= $cdata;
break;
}
}
else if($this->rdf_code == "image")
{
switch($this->current_tag)
{
case "URL" :
$this->channel['channel']['image_url'] .= $cdata;
break;
case "WIDTH" :
$this->channel['channel']['image_width'] .= $cdata;
break;
case "HEIGHT" :
$this->channel['channel']['image_height'] .= $cdata;
break;
case "TITLE" :
$this->channel['channel']['image_title'] .= $cdata;
break;
case "LINK" :
$this->channel['channel']['image_link'] .= $cdata;
break;
}
}
else if($this->rdf_code == "item")
{
switch($this->current_tag)
{
case "LINK" :
$this->channel['item'][$this->item_count]['link'] .= $cdata;
break;
case "TITLE" :
$this->channel['item'][$this->item_count]['title'] .= $cdata;
break;
case "DESCRIPTION" :
$this->channel['item'][$this->item_count]['description'] .= $cdata;
break;
case "PUBDATE" :
$this->channel['item'][$this->item_count]['pubdate'] .= $cdata;
break;
}
}
}
public function __destruct()
{
}
};
?>
-------------------------------------------------
소스2 (rss.html)
include_once("rss.reader.class.php");
/** using("xml");
**/
$rss = new rss();
try
{
// rss 주소
$rss->parse("http://slashdot.org/rss/index.rss");
}
catch(Exception $e)
{
die($e->getMessage());
}
?>
if(isset($rss->channel['channel'])) { ?>
} ?> | ||
|
한국의 주요 RSS 서비스
오마이뉴스 전체기사 : http://media.ohmynews.com/rss/ohmynews.xml
드림위즈 뉴스 : http://news.dreamwiz.com/rss/dw_rss_news.xml
중앙일보 : http://rss.joins.com/joins_news_list.xml
조선일보 : http://rss.chosun.com/rss.xml
Kbench : http://rss.kbench.com/kbench.xml
드림위즈 뉴스 : http://news.dreamwiz.com/rss/dw_rss_news.xml
중앙일보 : http://rss.joins.com/joins_news_list.xml
조선일보 : http://rss.chosun.com/rss.xml
Kbench : http://rss.kbench.com/kbench.xml
조인즈닷컴 (중앙일보) 리스트 : http://help.joins.com/conv/rss.html
드림위즈 리스트 : http://rss.dreamwiz.com/
조선일보 리스트 : http://www.chosun.com/rss/
드림위즈 리스트 : http://rss.dreamwiz.com/
조선일보 리스트 : http://www.chosun.com/rss/
'Computer > Etc' 카테고리의 다른 글
[RSS] 국내 RSS지원 되는사이트 (0) | 2005.08.02 |
---|---|
smi파일을 txt파일로 변환 (0) | 2005.08.01 |
탐색기의 특정폴더에서 도스프롬프트 열기 (0) | 2005.07.27 |
embed Object param name 배경음악 미디어 변경방법 (0) | 2005.06.21 |
msn 자동로그인 삭제/광고탭없애기 (0) | 2005.06.19 |