테스트 사이트 - 개발 중인 베타 버전입니다

[유용한팁]Kisa OpenAPI Whois 정보 (XML) 데이터 파싱하기

· 14년 전 · 1869
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>

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

게시글 목록

번호 제목
384
12508
383
381
20395
12507
12506
12505
12504
12503
12502
12500
12499
12498
20392
12497
12496
12495
12494
12493
12492
12491
12490
12489
12488