외부에 있는 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,297
15년 전 조회 678
15년 전 조회 735
15년 전 조회 905
15년 전 조회 918
15년 전 조회 772
15년 전 조회 1,354
15년 전 조회 863
15년 전 조회 1,091
15년 전 조회 1,480
15년 전 조회 968
15년 전 조회 985
15년 전 조회 892
15년 전 조회 843
15년 전 조회 1,183
15년 전 조회 933
15년 전 조회 816
15년 전 조회 1,196
15년 전 조회 2,347
15년 전 조회 934
15년 전 조회 1,058
15년 전 조회 1,190
15년 전 조회 913
15년 전 조회 961
15년 전 조회 1,005
15년 전 조회 879
15년 전 조회 967
15년 전 조회 2,452
15년 전 조회 1,765
15년 전 조회 6,053
15년 전 조회 1,303
15년 전 조회 1,577
15년 전 조회 1,938
15년 전 조회 3,659
15년 전 조회 2,998
15년 전 조회 2,072
15년 전 조회 1,581
15년 전 조회 4,024
15년 전 조회 1,106
15년 전 조회 885
15년 전 조회 1,063
15년 전 조회 992
15년 전 조회 986
15년 전 조회 970
15년 전 조회 926
15년 전 조회 1,056
15년 전 조회 856
15년 전 조회 854
15년 전 조회 740
15년 전 조회 4,491
15년 전 조회 958
15년 전 조회 671
15년 전 조회 1,081
15년 전 조회 1,050
15년 전 조회 1,178
15년 전 조회 851
15년 전 조회 979
15년 전 조회 960
15년 전 조회 919
15년 전 조회 806
15년 전 조회 862
15년 전 조회 989
15년 전 조회 797
15년 전 조회 799
15년 전 조회 882
15년 전 조회 813
15년 전 조회 790
15년 전 조회 804
15년 전 조회 796
15년 전 조회 814
15년 전 조회 761
15년 전 조회 772
15년 전 조회 744
15년 전 조회 963
15년 전 조회 898
15년 전 조회 761
15년 전 조회 779
15년 전 조회 705
15년 전 조회 1,878
15년 전 조회 921
15년 전 조회 725
15년 전 조회 867
15년 전 조회 767
15년 전 조회 905
15년 전 조회 767
15년 전 조회 866
15년 전 조회 831
15년 전 조회 756
15년 전 조회 915
15년 전 조회 708
15년 전 조회 847
15년 전 조회 907
15년 전 조회 1,189
15년 전 조회 780
15년 전 조회 836
15년 전 조회 854
15년 전 조회 1,435
15년 전 조회 806
15년 전 조회 958
15년 전 조회 1,357
🐛 버그신고