푸른청년 푸르게 살고있나?  
home | 살아가기 | news | 세상보기 | tip&tech | 방명록 |  
   전체
   asp
   php
   jsp
   mssql
   mysql
   informix
   linux
   unix
   win2000
   javascript
   html
   oracle
   java
   etc
    
:: Tip&Tech > php
pear패키지를 이용한 rss뉴스 읽기
먼저 pear패키지를 설치해야 한다.
무슨 패키지가 있는지 확인하는 방법은
[root@cms-img src]# /usr/local/php/bin/pear list

http://pear.php.net 에서 최신 패키지를 다운 받고
해당 서버에 올린다음 압축을 풀어 설치한다.
[root@cms-img src]# tar zvfx XML_Parser-1[1].2.6.tgz
[root@cms-img src]# cd XML_Parser-1[1].2.6
[root@cms-img src]# /usr/local/php/bin/pear install XML_Parser

[root@cms-img src]# tar zvfx XML_Tree-2.0.0RC2
[root@cms-img src]# cd XML_Tree-2.0.0RC2
[root@cms-img src]# /usr/local/php/bin/pear install XML_Tree

[root@cms-img src]# tar zvfx XML_RSS-0[1].9.2.tgz
[root@cms-img src]# cd XML_RSS-0[1].9.2.tgz
[root@cms-img src]# /usr/local/php/bin/pear install XML_RSS

주의 사항은 파일을 utf-8 타입으로 저장해야지 한글 파이일이 깨지지 않고 나온다.


<?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 "XML/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;

?>
<!-- 뉴스 아이템 -->
<li><a href="<?= $item[link] ?>"><?= $item[title] ?></a> (<?= $item[date] ?>) [<?= $item[cp_title] ?>] <p>
<?= $item[description] ?>
</li>
<!-- 뉴스 아이템 끝 -->
<?
}
$contents = ob_get_contents();
ob_end_flush();
unset($news);

set_modified($contents);
}

// description 교정
function parse_description($body) {
$current = array(
"<a href="
);
$target = array(
"<a target=\"_blank\" href="
);
$body = str_replace($current, $target, $body);
return $body;
}

// 마지막 수정 시간
function get_last_modified_time() {
$lastmodifiedtime = 0;
if(file_exists("newslastmodifiedtime.txt")) {
$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);
}
?>

예제 url이다
http://192.168.1.36/test/news_rss.php

날짜: 2005-09-05 15:29:05, 조회수: 65535

다음글 pear의 xmlrpc패키지를 이용한 리모트 디비에 인서트 하기
이전글 다차원 배열을 특정 필드로 정렬하기

꼬리말
글쓴이 비밀번호 #스팸글방지(주인장 닉네임을 쓰시오)

  
since by 2003.03.23 / 3th 2005.07.26 / 4th 2009.04.22 made by bluesoul