글
소켓통신 프로토콜, xml 파싱
<?
require_once "./config.php";
if($_POST[prc]=='add')
{
mem_go($m_id,$pw,$_POST[prc],$_POST[addid],'');
echo "$data";
}
else if($_POST[prc]=='del')
{
mem_go($m_id,$pw,$_POST[prc],$_POST[addid],$_POST[addpw]);
echo "$data";
}
unset($data);
unset($stack);
$stack = array();
function startTag($parser, $name, $attrs)
{
global $stack;
$tag=array("name"=>$name,"attrs"=>$attrs);
//print_r($tag);
//exit;
array_push($stack,$tag);
//print_r($stack);
//exit;
}
function cdata($parser, $cdata)
{
global $stack,$i;
if(trim($cdata))
{
$stack[count($stack)-1]['cdata']=$cdata;
}
}
function endTag($parser, $name)
{
global $stack;
$stack[count($stack)-2]['children'][] = $stack[count($stack)-1];
array_pop($stack);
}
function search_go($id,$pass,$page,$stxt)
{
global $data_xml,$stack,$top,$s_result;
$sp[host]=""; //domain or ip
$port=80;
$sp[path] = /result_01.php?m_d=$id&m_p=$pass&page=$page&search_txt=$stxt"; //디렉토리 경로.
$fsp = fsockopen ("$sp[host]", $port, $errno, $errstr, 30);
if (!$fsp)
{
echo "$errstr ($errno)<br>\n";
} else {
$refer = "Referer: $referer\r\n";
$request.="GET ".$sp[path]." HTTP/1.0\r\n";
$request.="Accept: */*\r\n";
$request.="Accept: ko\r\n";
$request.="Accept-Encoding: gzip, deflate\r\n";
$request.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2; .NET CLR 1.1.4322)\r\n";
$request.="Host: ".$sp["host"]."\r\n";
$request.=$refer;
$request.="Content-type: application/x-www-form-urlencoded\r\n";
$request.="Connection: close\r\n";
$request.="\r\n";
fputs($fsp , $request);
while(! feof ($fsp))
{ // 데이터 저장
$file .= fgets ($fsp, 1024);
}
//$file = 'simple.xml';
$tmp = explode('<?xml version="1.0" encoding="euc-kr"?>',$file);
$file = '<?xml version="1.0" encoding="euc-kr"?>' . $tmp[1];
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "cdata");
//$data = xml_parse($xml_parser,file_get_contents($file));
$data = xml_parse($xml_parser,$file);
if(!$data) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
xml_parser_free($xml_parser);
}
fclose ($fsp);
$top[totcnt] =$stack[0][children][0][cdata];
$top[totpag] =$stack[0][children][1][cdata];
$top[page] =$stack[0][children][2][cdata];
$top[pagecnt] =$stack[0][children][3][cdata];
$top[stxt] =$stack[0][children][4][cdata];
$top[no] =$top[totcnt]-($top[page]-1)*30;
$s_result = $stack[0][children][5][children];
if($top[no]>$top[pagecnt])$top[fnum]=$top[pagecnt];
return $top;
return $s_result;
}
function mem_go($id,$pass,$prc,$addid,$addpw)
{
global $file,$data;
$sp[host]=""; //domain or ip
$port=80;
$sp[path] = friend_search_proc.php?m_d=$id&m_p=$pass&prc=$prc&addid=$addid&addpw=$addpw"; //디렉토리 경로.
$fsp = fsockopen ("$sp[host]", $port, $errno, $errstr, 30);
if (!$fsp)
{
echo "$errstr ($errno)<br>\n";
} else {
$refer = "Referer: $referer\r\n";
$request.="GET ".$sp[path]." HTTP/1.0\r\n";
$request.="Accept: */*\r\n";
$request.="Accept: ko\r\n";
$request.="Accept-Encoding: gzip, deflate\r\n";
$request.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2; .NET CLR 1.1.4322)\r\n";
$request.="Host: ".$sp["host"]."\r\n";
$request.=$refer;
$request.="Content-type: application/x-www-form-urlencoded\r\n";
$request.="Connection: close\r\n";
$request.="\r\n";
fputs($fsp , $request);
while(! feof ($fsp))
{ // 데이터 저장
$file .= fgets ($fsp, 1024);
}
$tmp = explode("\r\n\r\n" , $file);
for ($i=1;$i<count($tmp);$i++)
{
$data .= $tmp[$i];
}
return $data;
}
}
//리스트 네이게이션 출력
function navi_print($tot , $e_limit , $page , $link="" , $icon=1)
{
/*
$tot : 총 레코드
$e_limit : 페이지당 출력 레코드
$page : 현재 페이지
$link : 다음페이지로 넘길 변수들(배열로 처리)
$icon : 아이콘셋
*/
if (intval($tot)<=0) return "";
$prev1[1] = "<img src=/images/btn_pre.gif border=0 align=absmiddle>";
$prev2[1] = "<img src=/images/btn_pre_2.gif border=0 align=absmiddle>";
$next1[1] = "<img src=/images/btn_next.gif border=0 align=absmiddle>";
$next2[1] = "<img src=/images/btn_next_2.gif border=0 align=absmiddle>";
$prev1[2] = "<img src=/images/bbs/bt_home.gif border=0 align=absmiddle>";
$prev2[2] = "<img src=/images/bbs/bt_pre.gif border=0 align=absmiddle>";
$next1[2] = "<img src=/images/bbs/bt_end.gif border=0 align=absmiddle>";
$next2[2] = "<img src=/images/bbs/bt_next.gif border=0 align=absmiddle>";
if (count($link)>0 && $link!="")
{
foreach ($link as $key => $value)
{
$url_link[] = "$key=" . urlencode($value);
}
$url_link = "&" . join("&",$url_link);
}
$pagenumber = 10;
$totalpage=intval($tot / $e_limit);
if (($tot % $e_limit)>0)
$totalpage++;
$cpage=$page;
$startpage = intval(($cpage - 1) / $pagenumber) * $pagenumber +1;
$endpage = intVal(((($startpage -1) +$pagenumber) / $pagenumber) * $pagenumber) ;
if ($totalpage <= $endpage)
$endpage = $totalpage;
if ( $cpage > $pagenumber)
{
$curpage = intval($startpage - 1);
$navi .=" <a href=\"$_SERVER[PHP_SELF]?page=1$url_link\">$prev1[$icon]</a> ";
$navi .=" <a href=\"$_SERVER[PHP_SELF]?page=$curpage$url_link\">$prev2[$icon]</a> ";
}
$curpage = $startpage;
while ($curpage <= $endpage):
$navi .= $curpage == $cpage ? " <font color=red>$cpage</font> " : " <a href=\"$_SERVER[PHP_SELF]?page=$curpage$url_link\">[$curpage]</a> ";
$curpage++;
endwhile ;
$navi .=" ...[<a href=\"$_SERVER[PHP_SELF]?page=$totalpage$url_link\">$totalpage</a>] ";
if ( $totalpage > $endpage)
{
$curpage = intval($endpage + 1);
$navi .= "<a href=\"$_SERVER[PHP_SELF]?page=$curpage$url_link\">$next2[$icon]</a>";
$navi .= "<a href=\"$_SERVER[PHP_SELF]?page=$totalpage$url_link\">$next1[$icon]</a>";
}
return $navi;
}
?>
'렛츠웹 + 게임 > My Tip' 카테고리의 다른 글
사운드제어 (0) | 2009.01.05 |
---|---|
엑셀로다운받기 (0) | 2009.01.05 |
키코드 만들기 (0) | 2009.01.05 |
PHP, Ajax (prototype)을 이용한 XML 통신 (0) | 2009.01.05 |
정규표현식(php) (0) | 2009.01.05 |