글
xml post로 보내고, 받아 처리
XML POST 로 보내고 받아 처리 하는 소스
php.ini에서 always_populate_raw_post_data = On
ini_set("always_populate_raw_post_data", "true");
보내는
<?php
/*--------------샘플 XML ---------------
<?xml version="1.0" encoding="EUC-KR"?>
<INFO>
<CONTENT>
<command>add</command>
<KEY>
<enswer_id>123</enswer_id>
</KEY>
<META>
<title>kim</title>
<director>tae</director>
</META>
<LICENSE>
<CP>
<name>jum</name>
<content_id>jjang</content_id>
</CP>
</LICENSE>
</CONTENT>
<CONTENT>
<command>add</command>
<KEY>
<enswer_id>456</enswer_id>
</KEY>
<META>
<title>kim2</title>
<director>tae2</director>
</META>
<LICENSE>
<CP>
<name>jum2</name>
<content_id>jjang2</content_id>
</CP>
</LICENSE>
</CONTENT>
</INFO>
--------------------------------------*/
// XML 데이터 생성 - 페이지 인코딩이 euc-kr이라 iconv로 UTF-8 형식으로 변환
$go_xml = '<?xml version="1.0" encoding="EUC-KR"?>
<INFO>
<CONTENT>
<command>add</command>
<KEY>
<enswer_id>123</enswer_id>
</KEY>
<META>
<title>kim</title>
<director>tae</director>
</META>
<LICENSE>
<CP>
<name>jum</name>
<content_id>jjang</content_id>
</CP>
</LICENSE>
</CONTENT>
<CONTENT>
<command>add</command>
<KEY>
<enswer_id>456</enswer_id>
</KEY>
<META>
<title>kim2</title>
<director>tae2</director>
</META>
<LICENSE>
<CP>
<name>jum2</name>
<content_id>jjang2</content_id>
</CP>
</LICENSE>
</CONTENT>
</INFO>';
// fsock으로 POST 전송
$host = '222...'; //'ip또는 도메인';
$path = '/파일명.php';
$xmlData = $go_xml;
// 헤더를 설정해서 POST로 전송
$fp = fsockopen($host, '80', $errno, $errstr, 30);
if($fp)
{
$header = "POST ".$path." HTTP/1.1\r\n";
$header .= "Host: ".$host."\r\n";
$header .= "User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n";
$header .= "Content-type: text/html\r\n";
$header .= "Content-length: ".strlen($xmlData)."\r\n\r\n";
$header .= $xmlData."\r\n";
fputs($fp, $header.$xmlData."\r\n\r\n");
while(!feof($fp))
{
$result .= fgets($fp, 1024);
}
fclose($fp);
echo $result; // 결과를 출력한다.
}
?>
받는
<?php
// raw_post_data 설정
ini_set("always_populate_raw_post_data", "true");
// xml 데이터를 받는다
$receive_xml = $GLOBALS['HTTP_RAW_POST_DATA'];
/*--------------------------------------
* XML Parse - simple_XML
----------------------------------------*/
$dom = new DOMDocument;
$dom->loadXML($receive_xml);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$XML = simplexml_import_dom($dom);
echo"<pre>";
print_r($XML);
$CONTENT=$XML->CONTENT;
$count = count($CONTENT);
for($i=0;$i<$count;$i++)
{
$command = iconv('UTF-8','EUC-KR',$CONTENT[$i]->command);
$enswer_id = iconv('UTF-8','EUC-KR',$CONTENT[$i]->KEY->enswer_id);
$title = iconv('UTF-8','EUC-KR',$CONTENT[$i]->META->title);
$director = iconv('UTF-8','EUC-KR',$CONTENT[$i]->META->director);
$name = iconv('UTF-8','EUC-KR',$CONTENT[$i]->LICENSE->CP->name);
$content_id = iconv('UTF-8','EUC-KR',$CONTENT[$i]->LICENSE->CP->name);
echo "<br>enswer_id-$enswer_id-$title-$director-$name-$content_id <br>";
}
?>
'렛츠웹 + 게임 > My Tip' 카테고리의 다른 글
추천 아이폰 무료 어플 2. (0) | 2010.01.27 |
---|---|
추천 아이폰 무료 어플 1. (0) | 2010.01.27 |
익스플로러에서 xml 유효성 체크 (0) | 2009.04.09 |
RIP와 IGRP (0) | 2009.01.22 |
IP 할당 체계와 IP 신청 방법 (0) | 2009.01.22 |