POST방식으로 특정 URL에 값 넘기기

 function postSend($url,$vars){  
 $result = array(); 
 $URL_parsed = parse_url($url) ;  
 $host = $URL_parsed["host"]; 
 $port = ($URL_parsed["port"]) ? $URL_parsed["port"] : 80; 
 $path = $URL_parsed["path"]; 
 
 $request.="POST ".$path." HTTP/1.1\r\n"; 
 $request.="Host: ".$host.":".$port."\r\n"; 
 $request.="Referer: $referer\r\n"; 
 $request.="Content-type: application/x-www-form-urlencoded\r\n"; 
 $request.="Content-length: ".strlen($vars)."\r\n"; 
 $request.="Connection: close\r\n"; 
 $request.="\r\n"; 
 $request.=$vars."\r\n"; 
 
 $fp = fsockopen($host, $port, $errno, $errstr, 30); 
 
 fputs($fp, $request); 
 while(!feof($fp)) { 
  $result[] .= fgets($fp, 1024); 
 } 
 fclose($fp); 
 
 return $result[10]; 
} 
 
//실제루틴
$url = "http://www.cena.co.kr/postTest.php"; 
$vars = "testvar1=test1&testvar2=test2"; 
$result = postSend($url,$vars); 
 
if($result == "OK"){ 
 echo "Server Return : OK" ;
} 

요지는 http 소켓을 만들때 저런 형태로 만들어서 보낸다는 것이다.

 

간단한 소스이므로 설명은 생략하며, 서버측에서 OK라는 신호를 찍어줬다면 아래의 echo가 찍힐것이다.

]]>

도큐멘트 에 올린 글

댓글 남기기

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.