메뉴 건너뛰기

프로그램언어

조회 수 3502 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

PHP GET,POST 요청시 응답결과를 소켓을 이용하여 PHP변수(문자열)로 받기

 

보통의 URL요청(브라우저 주소창에 홈페이지 주소 입력)은 요청주소 외에 전송데이터가 있든 없든 GET전송 요청이다.

POST전송은 <form>이 존재해야 하며 method를 post로 명시해야 한다.

 

이러한 <form>이 포함된 요청페이지를 서버에서 처리해야 하거나

요청 후 응답페이지를 브라우저로 보내기전에 가공해야할 필요가 있을때 fsockopen()을 이용한 웹접속을 이용하면 된다.


//GET 전송

function http_get($host, $path, $data, $cookie) {

    $http_response = "";

 

    $fp = fsockopen($host, 80);

 

    fputs($fp, "GET ".$path." HTTP/1.1\r\n");

    fputs($fp, "Host: ".$host."\r\n");

    fputs($fp, "User-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n");

    fputs($fp, "Referer: http://".$host.$path."\r\n");

    fputs($fp, "Cookie: ".$cookie."\r\n");

    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");

    fputs($fp, "Content-Length: ".strlen($data)."\r\n");

    fputs($fp, "Connection: close\r\n\r\n");

    fputs($fp, $data);

 

    while (!feof($fp)) {

        $http_response .= fgets($fp, 128);

    }

 

    fclose($fp);

 

    return $http_response;

}

 

$html = http_get("blog.naver.com", "/csaiur/220210546540", "", "");

echo "<xmp>".$html."</xmp>";



 

//POST 전송

function http_post($host, $path, $data, $cookie) {

    $http_response = "";

 

    $fp = fsockopen($host, 80);

 

    fputs($fp, "POST ".$path." HTTP/1.1\r\n");

    fputs($fp, "Host: ".$host."\r\n");

    fputs($fp, "User-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n");

    fputs($fp, "Referer: http://".$host.$path."\r\n");

    fputs($fp, "Cookie: ".$cookie."\r\n");

    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");

    fputs($fp, "Content-Length: ".strlen($data)."\r\n");

    fputs($fp, "Connection: close\r\n\r\n");

    fputs($fp, $data);

 

    while (!feof($fp)) {

        $http_response .= fgets($fp, 128);

    }

 

    fclose($fp);

 

    return $http_response;

}

 

$html = http_post("blog.naver.com", "/PostList.nhn", "blogId=csaiur&from=postList&categoryNo=12", "");

echo "<xmp>".$html."</xmp>";

 

 

//POST 전송 SSL

function https_post($host, $path, $data, $cookie) {

    $http_response = "";

    $errno = array();

    $errstr = array();

    $timeout = 30;

 

    $fp = fsockopen("ssl://".$host, 443, $errno, $errstr, $timeout);

 

    fputs($fp, "POST ".$path." HTTP/1.1\r\n");

    fputs($fp, "Host: ".$host."\r\n");

    fputs($fp, "User-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n");

    fputs($fp, "Referer: http://".$host.$path."\r\n");

    fputs($fp, "Cookie: ".$cookie."\r\n");

    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");

    fputs($fp, "Content-Length: ".strlen($data)."\r\n");

    fputs($fp, "Connection: close\r\n\r\n");

    fputs($fp, $data);

 

    while (!feof($fp)) {

        $http_response .= fgets($fp, 128);

    }

 

    fclose($fp);

 

    return $http_response;

}

전송하는 데이터가 없고 쿠키를 유지할 필요가 없거나 URL의 Response를 단순히 문자열로 받고자 할경우에는 fopen()을 이용해도 된다.

//요청URL의 결과를 가져와 문자열로 리턴

function get_text($url) {

    $text = "";

 

    if ($fp = @fopen($url, "r")) {

        while (!feof($fp)) {

            $s = fread($fp, 1);

            $text .= $s;

        }

        fclose($fp);

    }

 

    return $text;

}

 

$html = get_text("http://blog.naver.com/csaiur/220210546540");

echo "<xmp>".$html."</xmp>";


List of Articles
번호 제목 날짜 조회 수
220 array_slice 배열의 일부를 추출 2016.12.23 20775
219 [PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제 2017.02.19 35886
218 PHP에서 데이터를 엑셀(Excel)로 저장 2017.02.19 18428
217 파일업로드 2017.02.19 19352
216 phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성 file 2017.03.06 22787
215 PHPExcel 클래스를 이용해 Excel 2007~2010 의 xlsx 파일 읽기 (100만 행 까지) 2017.03.06 21694
214 CodeIgniter에서 PHPExcel 사용하기 file 2017.03.06 20289
213 PHP로 엑셀 자료 MySQL에 넣기 2017.03.06 17875
212 메일주소의 골뱅이를 그림처리하기 2017.03.06 15508
211 오류 메시지 출력(alert) 및 페이지 이동(refresh) 관련 2017.03.06 18568
210 ajax refresh 시키기(자동리플래쉬) with php file 2017.03.06 23185
209 Ajax로 구연한 실시간 서버시간출력 file 2017.03.06 21031
208 지엠 웹에디터 v1.1 (저작권표시없음)| file 2017.03.06 17109
207 text파일에 한줄씩 내용추가하기 2017.03.06 17533
206 PHP 만년달력 소스 2017.03.06 17063
205 PHP EXCEL export시 시트 이름 지정하여 여러 시트에 데이터 쓰기 2017.03.06 18266
204 PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때... 2017.03.06 16610
203 PHP에서 Excel 파일을 만들 수 있는 PHPExcel file 2017.03.06 17112
202 php로 db 컨트롤 1 2017.03.06 15769
201 php 문자열관련 함수 2017.03.06 15580
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 11 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved