[유용한팁]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>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 4430 | 13년 전 | 828 | ||
| 4429 | 13년 전 | 1154 | ||
| 4428 | 13년 전 | 1942 | ||
| 4427 | 13년 전 | 1060 | ||
| 4426 | 13년 전 | 522 | ||
| 4425 | 13년 전 | 9885 | ||
| 4424 |
|
13년 전 | 853 | |
| 4423 |
|
13년 전 | 732 | |
| 4422 |
aequum
|
13년 전 | 1301 | |
| 4421 | 13년 전 | 2418 | ||
| 4420 | 13년 전 | 1207 | ||
| 4419 | 13년 전 | 889 | ||
| 4418 |
한번잘해보자
|
13년 전 | 672 | |
| 4417 | 13년 전 | 1580 | ||
| 4416 | 13년 전 | 874 | ||
| 4415 | 13년 전 | 2257 | ||
| 4414 | 13년 전 | 684 | ||
| 4413 | 13년 전 | 681 | ||
| 4412 | 13년 전 | 822 | ||
| 4411 | 13년 전 | 1445 | ||
| 4410 |
|
13년 전 | 736 | |
| 4409 | 13년 전 | 2172 | ||
| 4408 |
visualp
|
13년 전 | 538 | |
| 4407 |
visualp
|
13년 전 | 3090 | |
| 4406 |
visualp
|
13년 전 | 3259 | |
| 4405 |
visualp
|
13년 전 | 3159 | |
| 4404 |
visualp
|
13년 전 | 2981 | |
| 4403 |
|
13년 전 | 672 | |
| 4402 |
gender
|
13년 전 | 629 | |
| 4401 | 13년 전 | 1122 | ||
| 4400 |
aequum
|
13년 전 | 1386 | |
| 4399 | 13년 전 | 593 | ||
| 4398 | 13년 전 | 674 | ||
| 4397 |
|
13년 전 | 591 | |
| 4396 |
aequum
|
13년 전 | 4666 | |
| 4395 |
|
13년 전 | 573 | |
| 4394 |
aequum
|
13년 전 | 4941 | |
| 4393 |
|
13년 전 | 1273 | |
| 4392 | 13년 전 | 1063 | ||
| 4391 |
mirrV
|
13년 전 | 544 | |
| 4390 |
파워웹프로
|
13년 전 | 711 | |
| 4389 | 13년 전 | 1231 | ||
| 4388 |
Coding
|
13년 전 | 710 | |
| 4387 |
aequum
|
13년 전 | 1367 | |
| 4386 | 13년 전 | 872 | ||
| 4385 | 13년 전 | 722 | ||
| 4384 | 13년 전 | 790 | ||
| 4383 | 13년 전 | 2904 | ||
| 4382 | 13년 전 | 617 | ||
| 4381 | 13년 전 | 1193 | ||
| 4380 | 13년 전 | 816 | ||
| 4379 |
|
13년 전 | 751 | |
| 4378 | 13년 전 | 665 | ||
| 4377 | 13년 전 | 3301 | ||
| 4376 |
aequum
|
13년 전 | 1219 | |
| 4375 |
클로로다인
|
13년 전 | 651 | |
| 4374 |
DDFACTORY
|
13년 전 | 721 | |
| 4373 |
까탈스런ET
|
13년 전 | 711 | |
| 4372 | 13년 전 | 814 | ||
| 4371 | 13년 전 | 576 | ||
| 4370 |
|
13년 전 | 653 | |
| 4369 |
프리프리닷
|
13년 전 | 1307 | |
| 4368 | 13년 전 | 3177 | ||
| 4367 |
soing
|
13년 전 | 1585 | |
| 4366 |
|
13년 전 | 686 | |
| 4365 |
|
13년 전 | 623 | |
| 4364 |
|
13년 전 | 760 | |
| 4363 |
|
13년 전 | 626 | |
| 4362 |
|
13년 전 | 731 | |
| 4361 |
|
13년 전 | 845 | |
| 4360 |
|
13년 전 | 651 | |
| 4359 |
|
13년 전 | 3129 | |
| 4358 |
|
13년 전 | 3043 | |
| 4357 | 13년 전 | 842 | ||
| 4356 | 13년 전 | 1346 | ||
| 4355 | 13년 전 | 969 | ||
| 4354 | 13년 전 | 820 | ||
| 4353 | 13년 전 | 3375 | ||
| 4352 | 13년 전 | 2378 | ||
| 4351 | 13년 전 | 1969 | ||
| 4350 |
|
13년 전 | 1888 | |
| 4349 | 13년 전 | 666 | ||
| 4348 |
aequum
|
13년 전 | 1431 | |
| 4347 | 13년 전 | 669 | ||
| 4346 |
|
13년 전 | 571 | |
| 4345 | 13년 전 | 599 | ||
| 4344 |
aequum
|
13년 전 | 1009 | |
| 4343 |
|
13년 전 | 1046 | |
| 4342 |
aequum
|
13년 전 | 1646 | |
| 4341 | 13년 전 | 813 | ||
| 4340 |
2번호랑이
|
13년 전 | 1012 | |
| 4339 |
|
13년 전 | 1174 | |
| 4338 | 13년 전 | 1170 | ||
| 4337 | 13년 전 | 521 | ||
| 4336 |
aequum
|
13년 전 | 1711 | |
| 4335 | 13년 전 | 896 | ||
| 4334 | 13년 전 | 1195 | ||
| 4333 |
Sturmvogel
|
13년 전 | 970 | |
| 4332 |
aequum
|
13년 전 | 1287 | |
| 4331 |
aequum
|
13년 전 | 1408 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기