반응형
File name : httpRequest.php
method =strtoupper($method);
$this->host =$host;
$this->port =$port;
$this->path =$path;
$this->errno =$errno;
$this->errstr =$errstr;
$this->timeout =$timeout;
$this->query = $query;
}
function openSocket() {
$this->fp = fsockopen($this->host,$this->port,$this->errno,$this->errstr,$this->timeout);
}
function closeSocket() {
fclose($this->fp);
}
// put header to connected socket
function putHeader() {
// if request method is GET, query attached the end of URL
$getQuery = $this->method == strtoupper("GET") ? "?".$this->query : "";
fputs($this->fp, $this->method." ".$this->path.$getQuery." / HTTP/1.1\r\n");
fputs($this->fp, "Host: ".$this->host."\r\n");
fputs($this->fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($this->fp, "Content-length: ".strlen($this->query)."\r\n");
fputs($this->fp, "Connection: close\r\n\r\n");
}
//put query to connected socket (only POST)
function putQuery() {
fputs($this->fp, $this->query . "\r\n\r\n");
}
// Gets responsed value(by line) from connected socket
function getResponse() {
while(!feof($this->fp))
{
$result .= fgets($this->fp, 1024);
}
$this->result = $result;
}
// partitioning Header,Body
function getResult() {
$tmp = explode("\r\n\r\n",$this->result); // Split a string by (CRLF *2)
$this->resultHeader = $tmp[0];
$this->resultBody = $tmp[1];
}
function sendRequest() {
$this->openSocket();
$this->putHeader();
if($this->method == strtoupper("POST")) $this->putQuery();
$this->getResponse();
$this->getResult();
$this->closeSocket();
}
}
?>
File name : shootRequest.php
sendRequest();
if($argv[2] == strtolower("h")) $result = $request->resultHeader; // print responsed header
elseif($argv[2] == strtolower("b")) $result = $request->resultBody; // print responsed body
else $result = $request->result; // print responsed all
print($result);
?>
위의 코드는 naver 검색페이지에서 특정 문장을 검색하는 코드입니다.
네이버 검색페이지는 외부에서 POST 지원을 않하는것 같습니다..
GET 으로 request를 날려 보겠습니다.
D:\Project\localhost >php shootRequest.php g h
파일명뒤의 g, h 옵션은~
g: GET 으로 쏘겠다는 것 입니다. (p 로 하시면 POST로 쏩니다.)
h: request에 대한 response 를 header 만 보겠다는 것 입니다.
(b 는 body, 아무것도 안넣으면 heaer + body)
대충 넣은 옵션 처리입니다. 코드 훓어 보면 아실겁니다.
물론 웹에서도 가능합니다. shootRequest.php 의 $method 를 "GET" 으로 고정시키고
웹에서 shootRequest.php 를 실행시키면 위의 코드가 어떤것인지 한눈에 알 수 있습니다.
반응형
'WEB언어 > PHP' 카테고리의 다른 글
| php 문자열 자르기 substr() 함수 explode() (0) | 2014.10.26 |
|---|---|
| php money_format 변환 - 미국 형식 화폐 / MySQL 일자포멧 / 요일확인 (0) | 2014.10.23 |
| php + mysql 웹 페이지 한글 깨짐 현상 처리 (0) | 2014.10.22 |
| [PHP] DB값 날짜 변경하기 / 나이 계산 (0) | 2014.05.25 |
| [php] regular expression - filter number only (0) | 2014.05.25 |
| [PHP] SSL 설정후 파일 다운로드 안되는 문제 (0) | 2013.07.03 |
| [PHP] 구분자로 split하기 (explode함수) (0) | 2012.09.06 |
| Fatal error: Cannot redeclare 메서드명() (previously declare의 해결방법 (0) | 2012.04.06 |
도움이 되셨다면 하트모양의 "♡ 공감"을 눌러주시면 큰 격려가 됩니다.
(로그인하지 않으셔도 가능)
(로그인하지 않으셔도 가능)