외부에 있는 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,299
15년 전 조회 683
15년 전 조회 738
15년 전 조회 906
15년 전 조회 919
15년 전 조회 776
15년 전 조회 1,357
15년 전 조회 866
15년 전 조회 1,094
15년 전 조회 1,483
15년 전 조회 974
15년 전 조회 988
15년 전 조회 896
15년 전 조회 847
15년 전 조회 1,184
15년 전 조회 936
15년 전 조회 818
15년 전 조회 1,198
15년 전 조회 2,348
15년 전 조회 935
15년 전 조회 1,063
15년 전 조회 1,193
15년 전 조회 916
15년 전 조회 967
15년 전 조회 1,008
15년 전 조회 880
15년 전 조회 969
15년 전 조회 2,454
15년 전 조회 1,768
15년 전 조회 6,058
15년 전 조회 1,305
15년 전 조회 1,579
15년 전 조회 1,940
15년 전 조회 3,662
15년 전 조회 2,999
15년 전 조회 2,075
15년 전 조회 1,582
15년 전 조회 4,026
15년 전 조회 1,108
15년 전 조회 889
15년 전 조회 1,066
15년 전 조회 993
15년 전 조회 990
15년 전 조회 975
15년 전 조회 931
15년 전 조회 1,058
15년 전 조회 859
15년 전 조회 854
15년 전 조회 744
15년 전 조회 4,493
15년 전 조회 963
15년 전 조회 674
15년 전 조회 1,082
15년 전 조회 1,051
15년 전 조회 1,182
15년 전 조회 855
15년 전 조회 980
15년 전 조회 961
15년 전 조회 922
15년 전 조회 810
15년 전 조회 867
15년 전 조회 992
15년 전 조회 802
15년 전 조회 803
15년 전 조회 882
15년 전 조회 817
15년 전 조회 792
15년 전 조회 808
15년 전 조회 797
15년 전 조회 818
15년 전 조회 764
15년 전 조회 774
15년 전 조회 747
15년 전 조회 966
15년 전 조회 901
15년 전 조회 764
15년 전 조회 782
15년 전 조회 708
15년 전 조회 1,882
15년 전 조회 927
15년 전 조회 727
15년 전 조회 869
15년 전 조회 772
15년 전 조회 908
15년 전 조회 770
15년 전 조회 867
15년 전 조회 834
15년 전 조회 759
15년 전 조회 919
15년 전 조회 709
15년 전 조회 848
15년 전 조회 909
15년 전 조회 1,191
15년 전 조회 784
15년 전 조회 838
15년 전 조회 859
15년 전 조회 1,443
15년 전 조회 809
15년 전 조회 959
15년 전 조회 1,357
🐛 버그신고