외부에 있는 xml을 파싱하는 예제

알바몬에서 던져주는 rss를 파싱하여 gnuboard의 특정 게시판에 입력하는 예제 입니다..

1. php5 에서만 작동합니다.

php4 에서는 new DOMDocument() 가 작동하지 않습니다. simplexml함수를 사용해야 하는데, 그거는 제가 약해서.

2. 알바몬쪽에서 던져준 소스는 euckr 인데, 제 서버는 utf-8로 해야 합니다.
근데 내부적으로 domdocument() 함수 자체에서 코딩을 이해해서 결과값 자체를 utf-8로 던져 주는 듯 합니다.
즉 내부적으로 언어를 utf-8로 쓰기 때문에. 인코딩 문제는 약간 테스트 해보셔야 할듯 합니다.

3. bo_table는 해당 게시판의 이름입니다. 실제로 작동하는 주소와 소스 이기 때문에 .. 테스트 할때 주의 하시기 바랍니다.

4. 알바몬의 서버는 윈도우 서버 더군요. 윈도우 서버와 리눅스 서버의 데이터 파싱 방법이 약간 틀립니다.



<?php
header("Content-type:text/html;charset=utf-8");
include_once './_common.php';

set_time_limit(0);
//파싱할 시간을 지정합니다 0은 30초 제한없이 완료 되는 시점 까지 입니다.

ini_set("memory_limit", "300M");
// 메모리 용량을 늘렸습니다. domdocument는 모든 xml을 메모리에 저장하기 때문에
// xml의 양이 많으면 느려지는 단점이 있습니다. 대신에 해당 xml이 부정확한 xml 형태라면 에러를 내줍니다.


$bo_table="sjob"; // 여기에서 해당 게시판을 적어 줍니다.

$url = "http://www.albamon.com/rss/rss_list.asp?scd=&gcd=&rbcd=3000&rpcd=3130&gubun=1";
//해당 주소는 알바정보중에서 피팅모델의 정보를 가져오는 주소입니다.

$html = 'html1';
// 이것은 내용을 입력할때 html을 그래도 인식시키기 위해서 써줍니다.


//윈도우의 경우에는 약간 틀리게 움직임.
//구분자가 \r\n\r\n이 아니고 \r\n 임.


//$page 구분자로 한다.



$result = fsockopen_func_albamon($url,true);
//이 함수의 사용은 밑에서 정의 하였습니다.

if($result):
$result ="<?xml ".$result;

$dom = new DOMDocument();

$dom->loadXML($result);

//item을 찾는다.
$items= $dom->getElementsByTagName("item");

//
foreach($items as $node){
$title = $node->getElementsByTagName("title");
$title =get_node_value($title);
$companys=explode("]", $title);
$company= trim(str_replace("[", '',$companys[0]));
$link = $node->getElementsByTagName("link");
$link = get_node_value($link);
$description = $node->getElementsByTagName("description");
$description = get_node_value($description);


$val['company'] = $company;
$val['subject'] = $title;
$val['move_url'] =$link;
$val['content'] = $description;


insert_table($val,$bo_table);
}

$dom=null;
endif;

function get_node_value($nodelist){
foreach($nodelist as $node){
return $node->nodeValue;
}
}

//쿼리를 구하는 부분
function insert_table($val, $bo_table){
$write_table ="g4_write_$bo_table";
$wr_num = get_next_num($write_table);
$wr_reply = "";

$wr_subject= $val['subject'];
$wr_content =$val['content'];
$wr_subject = addslashes($wr_subject);
$wr_content = addslashes($wr_content);
$wr_link1 = addslashes($val['move_url']);
$wr_1 = addslashes($val['move_url']);
//이동할 주소를 넣어 줍니다.즉 상세보기 주소.
//저는 wr_1에서 unique 인덱스 걸었습니다.
//그래야 중복으로 값이 입력되는 것을 방지 할 수 있으니깐요.


$wr_2 = 'albamon'; // 구분자를 넣어줍니다.
$wr_name= addslashes($val['company']);


$sql = " insert into $write_table
set wr_num = '$wr_num',
wr_reply = '$wr_reply',
wr_comment = 0,
ca_name = '$ca_name',
wr_option = '$html,$secret,$mail',
wr_subject = '$wr_subject',
wr_content = '$wr_content',
wr_link1 = '$wr_link1',
wr_link2 = '$wr_link2',
wr_link1_hit = 0,
wr_link2_hit = 0,
wr_trackback = '$wr_trackback',
wr_hit = 0,
wr_good = 0,
wr_nogood = 0,
mb_id = 'admin',
wr_password = 'admin_passd',
wr_name = '$wr_name',
wr_email = 'admin_mail',
wr_homepage = '',
wr_datetime = now(),
wr_last = now(),
wr_ip = '$_SERVER[REMOTE_ADDR]',
wr_1 = '$wr_1',
wr_2 = '$wr_2',
wr_3 = '$wr_3',
wr_4 = '$wr_4',
wr_5 = '$wr_5',
wr_6 = '$wr_6',
wr_7 = '$wr_7',
wr_8 = '$wr_8',
wr_9 = '$wr_9',
wr_10 = '$wr_10' ";


if(mysql_query($sql) == true):

$wr_id = mysql_insert_id();


if($wr_id==true):
// 부모 아이디에 UPDATE
sql_query(" update $write_table set wr_parent = '$wr_id' where wr_id = '$wr_id' ");

// 새글 INSERT
//sql_query(" insert into $g4[board_new_table] ( bo_table, wr_id, wr_parent, bn_datetime ) values ( '$bo_table', '$wr_id', '$wr_id', '$g4[time_ymdhis]' ) ");
sql_query(" insert into g4_board_new ( bo_table, wr_id, wr_parent, bn_datetime, mb_id ) values ( '$bo_table', '$wr_id', '$wr_id', now(), 'admin' ) ", false);

// 게시글 1 증가
sql_query("update g4_board set bo_count_write = bo_count_write + 1 where bo_table = '$bo_table'",false);
endif;
endif;
}

@mysql_close();


//알바몬을 파싱하는 경우
function fsockopen_func_albamon($url, $flag= true) {
$url_arr = parse_url($url);
$request = $url_arr['host'];
$get_value = $url_arr['path'].'?'.$url_arr['query'];
$fp = @fsockopen($request, 80, $errno, $errstr, 5);


if (!$fp) {
return false;
} else {
$out = "GET $get_value HTTP/1.1\r\n";
$out .= "Host: $request\r\n";
$out.= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$content .= fgets($fp, 128);
}
fclose($fp);

if(preg_match("/HTTP\/1\.1 200/", $content)==false)
return '';



$content_arr = explode("<?xml", $content);

return $content_arr[1];


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

댓글 3개

마침 찾고 있었는데 감사합니다.
알바몬이 euc-kr 이면 그냥 바로 euc-kr 그대로 파싱할려면 저부분에서 어딜 만져줘야하나염^^
외부에 있는 xml을 파싱하는 예제
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
15년 전 조회 1,304
15년 전 조회 687
15년 전 조회 746
15년 전 조회 909
15년 전 조회 928
15년 전 조회 782
15년 전 조회 1,361
15년 전 조회 870
15년 전 조회 1,098
15년 전 조회 1,491
15년 전 조회 977
15년 전 조회 991
15년 전 조회 899
15년 전 조회 853
15년 전 조회 1,191
15년 전 조회 942
15년 전 조회 824
15년 전 조회 1,206
15년 전 조회 2,354
15년 전 조회 943
15년 전 조회 1,069
15년 전 조회 1,198
15년 전 조회 921
15년 전 조회 972
15년 전 조회 1,016
15년 전 조회 885
15년 전 조회 976
15년 전 조회 2,458
15년 전 조회 1,773
15년 전 조회 6,063
15년 전 조회 1,311
15년 전 조회 1,586
15년 전 조회 1,947
15년 전 조회 3,671
15년 전 조회 3,003
15년 전 조회 2,079
15년 전 조회 1,587
15년 전 조회 4,032
15년 전 조회 1,111
15년 전 조회 894
15년 전 조회 1,073
15년 전 조회 999
15년 전 조회 995
15년 전 조회 981
15년 전 조회 936
15년 전 조회 1,064
15년 전 조회 865
15년 전 조회 863
15년 전 조회 749
15년 전 조회 4,498
15년 전 조회 965
15년 전 조회 680
15년 전 조회 1,088
15년 전 조회 1,059
15년 전 조회 1,188
15년 전 조회 864
15년 전 조회 989
15년 전 조회 969
15년 전 조회 930
15년 전 조회 816
15년 전 조회 873
15년 전 조회 1,000
15년 전 조회 808
15년 전 조회 809
15년 전 조회 884
15년 전 조회 822
15년 전 조회 800
15년 전 조회 810
15년 전 조회 802
15년 전 조회 820
15년 전 조회 772
15년 전 조회 778
15년 전 조회 753
15년 전 조회 971
15년 전 조회 907
15년 전 조회 767
15년 전 조회 789
15년 전 조회 714
15년 전 조회 1,886
15년 전 조회 929
15년 전 조회 731
15년 전 조회 876
15년 전 조회 779
15년 전 조회 913
15년 전 조회 776
15년 전 조회 875
15년 전 조회 837
15년 전 조회 764
15년 전 조회 925
15년 전 조회 719
15년 전 조회 853
15년 전 조회 913
15년 전 조회 1,200
15년 전 조회 786
15년 전 조회 845
15년 전 조회 862
15년 전 조회 1,447
15년 전 조회 813
15년 전 조회 965
15년 전 조회 1,362
🐛 버그신고