외부에 있는 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을 파싱하는 예제
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

태그 필터 (최대 3개) 전체 개발자 소스 기타 mysql 팁자료실 javascript php linux flash 정규표현식 jquery node.js mobile 웹서버 os 프로그램 강좌 썸네일 이미지관련 도로명주소 그누보드5 기획자 견적서 계약서 기획서 마케팅 제안서 seo 통계 서식 통계자료 퍼블리셔 html css 반응형 웹접근성 퍼블리싱 표준화 반응형웹 홈페이지기초 부트스트랩 angularjs 포럼 스크린리더 센스리더 개발자톡 개발자팁 퍼블리셔톡 퍼블리셔팁 기획자톡 기획자팁 프로그램강좌 퍼블리싱강좌
+
제목 글쓴이 날짜 조회
15년 전 조회 1,316
15년 전 조회 693
15년 전 조회 762
15년 전 조회 925
15년 전 조회 940
15년 전 조회 792
15년 전 조회 1,372
15년 전 조회 884
15년 전 조회 1,114
15년 전 조회 1,504
15년 전 조회 987
15년 전 조회 1,006
15년 전 조회 908
15년 전 조회 861
15년 전 조회 1,210
15년 전 조회 954
15년 전 조회 833
15년 전 조회 1,220
15년 전 조회 2,370
15년 전 조회 956
15년 전 조회 1,081
15년 전 조회 1,215
15년 전 조회 935
15년 전 조회 983
15년 전 조회 1,032
15년 전 조회 901
15년 전 조회 988
15년 전 조회 2,477
15년 전 조회 1,785
15년 전 조회 6,077
15년 전 조회 1,327
15년 전 조회 1,600
15년 전 조회 1,960
15년 전 조회 3,683
15년 전 조회 3,023
15년 전 조회 2,088
15년 전 조회 1,604
15년 전 조회 4,049
15년 전 조회 1,128
15년 전 조회 906
15년 전 조회 1,081
15년 전 조회 1,016
15년 전 조회 1,007
15년 전 조회 986
15년 전 조회 948
15년 전 조회 1,076
15년 전 조회 877
15년 전 조회 875
15년 전 조회 757
15년 전 조회 4,514
15년 전 조회 983
15년 전 조회 690
15년 전 조회 1,101
15년 전 조회 1,073
15년 전 조회 1,200
15년 전 조회 878
15년 전 조회 999
15년 전 조회 984
15년 전 조회 940
15년 전 조회 830
15년 전 조회 889
15년 전 조회 1,017
15년 전 조회 815
15년 전 조회 824
15년 전 조회 890
15년 전 조회 832
15년 전 조회 810
15년 전 조회 822
15년 전 조회 814
15년 전 조회 834
15년 전 조회 786
15년 전 조회 787
15년 전 조회 760
15년 전 조회 982
15년 전 조회 919
15년 전 조회 780
15년 전 조회 799
15년 전 조회 728
15년 전 조회 1,898
15년 전 조회 938
15년 전 조회 747
15년 전 조회 884
15년 전 조회 786
15년 전 조회 928
15년 전 조회 787
15년 전 조회 884
15년 전 조회 850
15년 전 조회 778
15년 전 조회 941
15년 전 조회 730
15년 전 조회 865
15년 전 조회 920
15년 전 조회 1,211
15년 전 조회 799
15년 전 조회 856
15년 전 조회 873
15년 전 조회 1,451
15년 전 조회 826
15년 전 조회 980
15년 전 조회 1,368