외부에 있는 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,287
15년 전 조회 669
15년 전 조회 731
15년 전 조회 895
15년 전 조회 906
15년 전 조회 765
15년 전 조회 1,345
15년 전 조회 855
15년 전 조회 1,084
15년 전 조회 1,474
15년 전 조회 960
15년 전 조회 978
15년 전 조회 885
15년 전 조회 835
15년 전 조회 1,176
15년 전 조회 928
15년 전 조회 805
15년 전 조회 1,188
15년 전 조회 2,341
15년 전 조회 929
15년 전 조회 1,052
15년 전 조회 1,185
15년 전 조회 905
15년 전 조회 951
15년 전 조회 998
15년 전 조회 871
15년 전 조회 959
15년 전 조회 2,446
15년 전 조회 1,758
15년 전 조회 6,046
15년 전 조회 1,296
15년 전 조회 1,570
15년 전 조회 1,932
15년 전 조회 3,654
15년 전 조회 2,990
15년 전 조회 2,066
15년 전 조회 1,573
15년 전 조회 4,019
15년 전 조회 1,098
15년 전 조회 879
15년 전 조회 1,054
15년 전 조회 982
15년 전 조회 978
15년 전 조회 962
15년 전 조회 920
15년 전 조회 1,049
15년 전 조회 846
15년 전 조회 845
15년 전 조회 732
15년 전 조회 4,485
15년 전 조회 948
15년 전 조회 659
15년 전 조회 1,071
15년 전 조회 1,041
15년 전 조회 1,172
15년 전 조회 844
15년 전 조회 970
15년 전 조회 954
15년 전 조회 912
15년 전 조회 799
15년 전 조회 857
15년 전 조회 981
15년 전 조회 788
15년 전 조회 790
15년 전 조회 881
15년 전 조회 804
15년 전 조회 779
15년 전 조회 795
15년 전 조회 786
15년 전 조회 806
15년 전 조회 752
15년 전 조회 766
15년 전 조회 736
15년 전 조회 956
15년 전 조회 890
15년 전 조회 753
15년 전 조회 771
15년 전 조회 698
15년 전 조회 1,870
15년 전 조회 914
15년 전 조회 716
15년 전 조회 862
15년 전 조회 763
15년 전 조회 897
15년 전 조회 759
15년 전 조회 856
15년 전 조회 819
15년 전 조회 744
15년 전 조회 908
15년 전 조회 700
15년 전 조회 840
15년 전 조회 897
15년 전 조회 1,184
15년 전 조회 774
15년 전 조회 829
15년 전 조회 846
15년 전 조회 1,428
15년 전 조회 800
15년 전 조회 952
15년 전 조회 1,356
🐛 버그신고