[유용한팁]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>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7730 | 10년 전 | 1314 | ||
| 7729 | 10년 전 | 1172 | ||
| 7728 |
잘살아보자
|
10년 전 | 614 | |
| 7727 |
잘살아보자
|
10년 전 | 510 | |
| 7726 |
잘살아보자
|
10년 전 | 841 | |
| 7725 |
잘살아보자
|
10년 전 | 567 | |
| 7724 |
잘살아보자
|
10년 전 | 484 | |
| 7723 |
잘살아보자
|
10년 전 | 548 | |
| 7722 |
잘살아보자
|
10년 전 | 487 | |
| 7721 |
잘살아보자
|
10년 전 | 518 | |
| 7720 |
잘살아보자
|
10년 전 | 483 | |
| 7719 |
비긴어게인
|
10년 전 | 701 | |
| 7718 |
|
10년 전 | 2548 | |
| 7717 |
잘살아보자
|
10년 전 | 669 | |
| 7716 |
잘살아보자
|
10년 전 | 413 | |
| 7715 |
잘살아보자
|
10년 전 | 445 | |
| 7714 |
잘살아보자
|
10년 전 | 510 | |
| 7713 | 10년 전 | 1791 | ||
| 7712 | 10년 전 | 1730 | ||
| 7711 | 10년 전 | 1115 | ||
| 7710 | 10년 전 | 1408 | ||
| 7709 | 10년 전 | 1528 | ||
| 7708 | 10년 전 | 1468 | ||
| 7707 | 10년 전 | 866 | ||
| 7706 |
별지기천사
|
10년 전 | 580 | |
| 7705 | 10년 전 | 1085 | ||
| 7704 |
ICONdesignstudio
|
10년 전 | 638 | |
| 7703 | 10년 전 | 604 | ||
| 7702 |
|
10년 전 | 742 | |
| 7701 | 10년 전 | 1430 | ||
| 7700 | 10년 전 | 1116 | ||
| 7699 | 10년 전 | 586 | ||
| 7698 | 10년 전 | 1158 | ||
| 7697 | 10년 전 | 5180 | ||
| 7696 | 10년 전 | 672 | ||
| 7695 | 10년 전 | 1697 | ||
| 7694 | 10년 전 | 1080 | ||
| 7693 | 10년 전 | 1568 | ||
| 7692 | 10년 전 | 1312 | ||
| 7691 | 10년 전 | 836 | ||
| 7690 | 10년 전 | 1398 | ||
| 7689 | 10년 전 | 1028 | ||
| 7688 | 10년 전 | 633 | ||
| 7687 |
파랑새1597
|
10년 전 | 613 | |
| 7686 | 10년 전 | 864 | ||
| 7685 | 10년 전 | 1351 | ||
| 7684 | 10년 전 | 811 | ||
| 7683 | 10년 전 | 1118 | ||
| 7682 | 10년 전 | 1038 | ||
| 7681 | 10년 전 | 681 | ||
| 7680 | 10년 전 | 998 | ||
| 7679 | 10년 전 | 521 | ||
| 7678 | 10년 전 | 750 | ||
| 7677 | 10년 전 | 650 | ||
| 7676 |
|
10년 전 | 958 | |
| 7675 |
|
10년 전 | 1191 | |
| 7674 | 10년 전 | 1057 | ||
| 7673 | 10년 전 | 764 | ||
| 7672 | 10년 전 | 1104 | ||
| 7671 | 10년 전 | 915 | ||
| 7670 | 10년 전 | 682 | ||
| 7669 |
mashmellow
|
10년 전 | 1240 | |
| 7668 | 10년 전 | 729 | ||
| 7667 | 10년 전 | 1026 | ||
| 7666 |
senseme
|
10년 전 | 668 | |
| 7665 | 10년 전 | 523 | ||
| 7664 | 10년 전 | 1907 | ||
| 7663 |
mixx애교
|
10년 전 | 988 | |
| 7662 | 10년 전 | 1056 | ||
| 7661 |
hkhkah
|
10년 전 | 801 | |
| 7660 | 10년 전 | 1076 | ||
| 7659 |
커네드커네드
|
10년 전 | 946 | |
| 7658 |
바람돌이팡
|
10년 전 | 686 | |
| 7657 | 10년 전 | 1175 | ||
| 7656 | 10년 전 | 1587 | ||
| 7655 | 10년 전 | 1005 | ||
| 7654 |
개발짜증나
|
10년 전 | 868 | |
| 7653 |
네이비칼라
|
10년 전 | 889 | |
| 7652 |
밥먹고합시다
|
10년 전 | 819 | |
| 7651 |
플라이SINJI
|
10년 전 | 1522 | |
| 7650 |
개발짜증나
|
10년 전 | 1430 | |
| 7649 | 10년 전 | 460 | ||
| 7648 |
이미영ㅇㅇ
|
10년 전 | 881 | |
| 7647 | 10년 전 | 453 | ||
| 7646 | 10년 전 | 823 | ||
| 7645 | 10년 전 | 2333 | ||
| 7644 | 10년 전 | 829 | ||
| 7643 |
|
10년 전 | 2882 | |
| 7642 | 10년 전 | 1527 | ||
| 7641 | 10년 전 | 1150 | ||
| 7640 |
개발짜증나
|
10년 전 | 479 | |
| 7639 |
|
10년 전 | 823 | |
| 7638 |
개발짜증나
|
10년 전 | 1144 | |
| 7637 | 10년 전 | 1565 | ||
| 7636 | 10년 전 | 2921 | ||
| 7635 | 10년 전 | 1711 | ||
| 7634 | 10년 전 | 1899 | ||
| 7633 | 10년 전 | 2358 | ||
| 7632 | 10년 전 | 3966 | ||
| 7631 |
|
10년 전 | 1554 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기