[유용한팁]Kisa OpenAPI Whois 정보 (XML) 데이터 파싱하기
class XmlParser
{
public $parser;
public $depth=0;
public $termStack;
public $nodeData;
public $fullParseData;
public $prevdepth;
public $uri;
public $last_node;
public $inside_data;
function XmlParser($uri)
{
$this->setURI($uri);
$this->run();
}
function run()
{
$this->termStack = array();
$this->xmlInit();
$this->parsing();
}
function setURI($uri)
{
$this->uri = $uri;
}
function xmlInit()
{
$this->parser = xml_parser_create();
if(!$this->parser) echo "Parser Error<br>";
if(!xml_set_object($this->parser, $this)) echo "xml set object error<br>";
if(!xml_set_element_handler($this->parser, "tag_open", "tag_close")) echo "handler set error<br>";
if(!xml_set_character_data_handler($this->parser, "cdata")) echo "cdata handler error<br>";
}
function cdata($parser, $cdata)
{
if($this->depth > $this->prevdepth)
{
if($this->inside_data)
$this->nodeData[$this->nodeName()] .= $cdata;
else
$this->nodeData[$this->nodeName()] = $cdata;
$this->last_node = $this->nodeName();
}
$this->inside_data=true;
}
function getData($node=null)
{
if($node == null)
{
return $this->fullParseData;
}
return $this->fullParseData[$node];
}
function parsing()
{
$fp = fopen($this->uri, "r");
if(!$fp)
{
return 0;
}
while($data = fread($fp, 9182))
{
$this->parse($data);
}
fclose($fp);
return 1;
}
function parse($data)
{
if(!xml_parse($this->parser, $data)) echo xml_error_string(xml_get_error_code($this->parser));
}
function getpNode($depth=0)
{
if($depth != 0)
{
$node=count($this->termStack) + $depth;
$stack = array_slice($this->termStack, 0, $node);
}
else
{
$stack = $this->termStack;
}
return join("/",$stack);
}
function pushStack($name)
{
array_push($this->termStack, $name);
}
function getStackSize()
{
return count($this->termStack);
}
function tag_open($parser, $tag, $attributes)
{
$this->pushStack($tag);
if($this->depth > $this->prevdepth)
{
if(count($this->nodeData))
{
$last_node = $this->getpNode(-2);
$this->fullParseData[$last_node] = $this->nodeData;
}
$this->nodeData=array();
$this->prevdepth = $this->depth;
}
$this->depth++;
$this->inside_data=false;
}
function tag_close($parser, $tag)
{
$count = count($this->nodeData);
if($count == 0)
array_pop($this->termStack);
$this->depth--;
if($this->depth < $this->prevdepth)
{
if(count($this->nodeData) > 1)
$this->fullParseData[$this->getpNode()][] = $this->nodeData;
else
$this->fullParseData[$this->getpNode()] = $this->nodeData;
$this->nodeData=array();
}
else
{
$this->prevdepth = $this->depth;
}
if($count != 0)
array_pop($this->termStack);
$this->inside_data=false;
}
function nodeName()
{
return $this->termStack[$this->depth-1];
}
}
$parser = new XmlParser('http://whois.kisa.or.kr/openapi/whois.jsp?query='.$ip.'&key=키값');
//print_r($parser);
$items = $parser->getData('WHOIS/KOREAN/USER/TECHCONTACT');
//print_r($items);
foreach($items as $item)
{
$name = $item['NAME'];
$addr = $item['ADDR'];
$phone = $item['PHONE'];
$email = $item['EMAIL'];
}
-----------------------------------------------------------------------------------------------------------
위의 소스는 kisa 에서 지원하는 OpenAPI 의 키를 우선 등록하셔서 사용할 수 있습니다.
선행되어야 하는 작업은 ip 를 받는것입니다.
뭐 $_REQUEST 로 받은 REMOTE_ADDR 로 받든 ip 를 받아내서 그 정보를
kisa 쪽으로 전달해주면 xml 로 return 받을것입니다.
그 값을 저희는 짤라서 쓰기 편하게 파싱작업을 하는것이구요.
foreach 문에서 받은 데이터의 배열을 쓰기편하게 짤라주면 됩니다.
굳이 수정을 한다면 아래 부분만 고쳐 쓰면 됩니다.
$parser = new XmlParser('http://whois.kisa.or.kr/openapi/whois.jsp?query='.$ip.'&key=키값'); // kisa 서버 경로
//print_r($parser);
$items = $parser->getData('WHOIS/KOREAN/USER/TECHCONTACT'); //xml 구조
//print_r($items);
foreach($items as $item)
{
$name = $item['NAME']; //데이터들
$addr = $item['ADDR'];
$phone = $item['PHONE'];
$email = $item['EMAIL'];
}
혹 작업간에 에러가 나거나 출력이 잘 안될때를 대비해 어디서든 디코드가 가능하도록 print_r 사용의 생활화가 되어야 합니다.
늘 주석으로 꼭 포함해서 소스 확인하는 습관을 가지면 더욱 좋습니다.
그럼 이만..<div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>
{
public $parser;
public $depth=0;
public $termStack;
public $nodeData;
public $fullParseData;
public $prevdepth;
public $uri;
public $last_node;
public $inside_data;
function XmlParser($uri)
{
$this->setURI($uri);
$this->run();
}
function run()
{
$this->termStack = array();
$this->xmlInit();
$this->parsing();
}
function setURI($uri)
{
$this->uri = $uri;
}
function xmlInit()
{
$this->parser = xml_parser_create();
if(!$this->parser) echo "Parser Error<br>";
if(!xml_set_object($this->parser, $this)) echo "xml set object error<br>";
if(!xml_set_element_handler($this->parser, "tag_open", "tag_close")) echo "handler set error<br>";
if(!xml_set_character_data_handler($this->parser, "cdata")) echo "cdata handler error<br>";
}
function cdata($parser, $cdata)
{
if($this->depth > $this->prevdepth)
{
if($this->inside_data)
$this->nodeData[$this->nodeName()] .= $cdata;
else
$this->nodeData[$this->nodeName()] = $cdata;
$this->last_node = $this->nodeName();
}
$this->inside_data=true;
}
function getData($node=null)
{
if($node == null)
{
return $this->fullParseData;
}
return $this->fullParseData[$node];
}
function parsing()
{
$fp = fopen($this->uri, "r");
if(!$fp)
{
return 0;
}
while($data = fread($fp, 9182))
{
$this->parse($data);
}
fclose($fp);
return 1;
}
function parse($data)
{
if(!xml_parse($this->parser, $data)) echo xml_error_string(xml_get_error_code($this->parser));
}
function getpNode($depth=0)
{
if($depth != 0)
{
$node=count($this->termStack) + $depth;
$stack = array_slice($this->termStack, 0, $node);
}
else
{
$stack = $this->termStack;
}
return join("/",$stack);
}
function pushStack($name)
{
array_push($this->termStack, $name);
}
function getStackSize()
{
return count($this->termStack);
}
function tag_open($parser, $tag, $attributes)
{
$this->pushStack($tag);
if($this->depth > $this->prevdepth)
{
if(count($this->nodeData))
{
$last_node = $this->getpNode(-2);
$this->fullParseData[$last_node] = $this->nodeData;
}
$this->nodeData=array();
$this->prevdepth = $this->depth;
}
$this->depth++;
$this->inside_data=false;
}
function tag_close($parser, $tag)
{
$count = count($this->nodeData);
if($count == 0)
array_pop($this->termStack);
$this->depth--;
if($this->depth < $this->prevdepth)
{
if(count($this->nodeData) > 1)
$this->fullParseData[$this->getpNode()][] = $this->nodeData;
else
$this->fullParseData[$this->getpNode()] = $this->nodeData;
$this->nodeData=array();
}
else
{
$this->prevdepth = $this->depth;
}
if($count != 0)
array_pop($this->termStack);
$this->inside_data=false;
}
function nodeName()
{
return $this->termStack[$this->depth-1];
}
}
$parser = new XmlParser('http://whois.kisa.or.kr/openapi/whois.jsp?query='.$ip.'&key=키값');
//print_r($parser);
$items = $parser->getData('WHOIS/KOREAN/USER/TECHCONTACT');
//print_r($items);
foreach($items as $item)
{
$name = $item['NAME'];
$addr = $item['ADDR'];
$phone = $item['PHONE'];
$email = $item['EMAIL'];
}
-----------------------------------------------------------------------------------------------------------
위의 소스는 kisa 에서 지원하는 OpenAPI 의 키를 우선 등록하셔서 사용할 수 있습니다.
선행되어야 하는 작업은 ip 를 받는것입니다.
뭐 $_REQUEST 로 받은 REMOTE_ADDR 로 받든 ip 를 받아내서 그 정보를
kisa 쪽으로 전달해주면 xml 로 return 받을것입니다.
그 값을 저희는 짤라서 쓰기 편하게 파싱작업을 하는것이구요.
foreach 문에서 받은 데이터의 배열을 쓰기편하게 짤라주면 됩니다.
굳이 수정을 한다면 아래 부분만 고쳐 쓰면 됩니다.
$parser = new XmlParser('http://whois.kisa.or.kr/openapi/whois.jsp?query='.$ip.'&key=키값'); // kisa 서버 경로
//print_r($parser);
$items = $parser->getData('WHOIS/KOREAN/USER/TECHCONTACT'); //xml 구조
//print_r($items);
foreach($items as $item)
{
$name = $item['NAME']; //데이터들
$addr = $item['ADDR'];
$phone = $item['PHONE'];
$email = $item['EMAIL'];
}
혹 작업간에 에러가 나거나 출력이 잘 안될때를 대비해 어디서든 디코드가 가능하도록 print_r 사용의 생활화가 되어야 합니다.
늘 주석으로 꼭 포함해서 소스 확인하는 습관을 가지면 더욱 좋습니다.
그럼 이만..<div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 4130 |
진정한승리v
|
13년 전 | 1054 | |
| 4129 | 13년 전 | 1421 | ||
| 4128 |
onlymilk74
|
13년 전 | 612 | |
| 4127 | 13년 전 | 529 | ||
| 4126 |
visualp
|
13년 전 | 1245 | |
| 4125 |
visualp
|
13년 전 | 1579 | |
| 4124 | 13년 전 | 1650 | ||
| 4123 | 13년 전 | 819 | ||
| 4122 |
visualp
|
13년 전 | 633 | |
| 4121 |
visualp
|
13년 전 | 1788 | |
| 4120 |
visualp
|
13년 전 | 855 | |
| 4119 |
visualp
|
13년 전 | 1230 | |
| 4118 |
그래픽노블
|
13년 전 | 682 | |
| 4117 |
visualp
|
13년 전 | 742 | |
| 4116 | 13년 전 | 731 | ||
| 4115 |
visualp
|
13년 전 | 823 | |
| 4114 |
onlymilk74
|
13년 전 | 4623 | |
| 4113 | 13년 전 | 730 | ||
| 4112 |
그래픽노블
|
13년 전 | 850 | |
| 4111 | 13년 전 | 1505 | ||
| 4110 | 13년 전 | 673 | ||
| 4109 | 13년 전 | 467 | ||
| 4108 | 13년 전 | 841 | ||
| 4107 | 13년 전 | 2112 | ||
| 4106 | 13년 전 | 1584 | ||
| 4105 |
onlymilk74
|
13년 전 | 1406 | |
| 4104 | 14년 전 | 2836 | ||
| 4103 | 14년 전 | 1947 | ||
| 4102 | 14년 전 | 903 | ||
| 4101 | 14년 전 | 970 | ||
| 4100 | 14년 전 | 926 | ||
| 4099 | 14년 전 | 1023 | ||
| 4098 |
Lonnie
|
14년 전 | 517 | |
| 4097 | 14년 전 | 820 | ||
| 4096 | 14년 전 | 970 | ||
| 4095 | 14년 전 | 2521 | ||
| 4094 | 14년 전 | 836 | ||
| 4093 | 14년 전 | 565 | ||
| 4092 |
|
14년 전 | 586 | |
| 4091 | 14년 전 | 2934 | ||
| 4090 | 14년 전 | 722 | ||
| 4089 |
|
14년 전 | 1437 | |
| 4088 | 14년 전 | 1439 | ||
| 4087 | 14년 전 | 634 | ||
| 4086 | 14년 전 | 1356 | ||
| 4085 | 14년 전 | 747 | ||
| 4084 | 14년 전 | 851 | ||
| 4083 | 14년 전 | 1815 | ||
| 4082 | 14년 전 | 1520 | ||
| 4081 | 14년 전 | 2120 | ||
| 4080 |
onlymilk74
|
14년 전 | 854 | |
| 4079 | 14년 전 | 740 | ||
| 4078 | 14년 전 | 2124 | ||
| 4077 |
DreamT
|
14년 전 | 755 | |
| 4076 | 14년 전 | 861 | ||
| 4075 | 14년 전 | 1974 | ||
| 4074 | 14년 전 | 991 | ||
| 4073 | 14년 전 | 900 | ||
| 4072 |
onlymilk74
|
14년 전 | 649 | |
| 4071 | 14년 전 | 896 | ||
| 4070 | 14년 전 | 1911 | ||
| 4069 | 14년 전 | 476 | ||
| 4068 | 14년 전 | 2436 | ||
| 4067 | 14년 전 | 802 | ||
| 4066 | 14년 전 | 509 | ||
| 4065 | 14년 전 | 514 | ||
| 4064 | 14년 전 | 774 | ||
| 4063 | 14년 전 | 683 | ||
| 4062 | 14년 전 | 579 | ||
| 4061 | 14년 전 | 1072 | ||
| 4060 | 14년 전 | 510 | ||
| 4059 | 14년 전 | 1190 | ||
| 4058 | 14년 전 | 1558 | ||
| 4057 | 14년 전 | 519 | ||
| 4056 |
|
14년 전 | 653 | |
| 4055 |
SGFlash
|
14년 전 | 507 | |
| 4054 |
Priere
|
14년 전 | 658 | |
| 4053 | 14년 전 | 1085 | ||
| 4052 | 14년 전 | 781 | ||
| 4051 | 14년 전 | 906 | ||
| 4050 | 14년 전 | 726 | ||
| 4049 | 14년 전 | 863 | ||
| 4048 |
내꿈은대통령
|
14년 전 | 456 | |
| 4047 |
visualp
|
14년 전 | 1265 | |
| 4046 |
visualp
|
14년 전 | 608 | |
| 4045 |
visualp
|
14년 전 | 1369 | |
| 4044 | 14년 전 | 8124 | ||
| 4043 | 14년 전 | 746 | ||
| 4042 | 14년 전 | 1592 | ||
| 4041 | 14년 전 | 1317 | ||
| 4040 | 14년 전 | 1592 | ||
| 4039 | 14년 전 | 1927 | ||
| 4038 | 14년 전 | 526 | ||
| 4037 |
sider
|
14년 전 | 670 | |
| 4036 | 14년 전 | 6462 | ||
| 4035 | 14년 전 | 652 | ||
| 4034 | 14년 전 | 584 | ||
| 4033 |
techer
|
14년 전 | 1731 | |
| 4032 | 14년 전 | 639 | ||
| 4031 | 14년 전 | 634 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기