메뉴 건너뛰기

프로그램언어

조회 수 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>";


  1. No Image 29Aug
    by
    2018/08/29 Views 3933 

    한글이 깨져서 나올 때 - iconv

  2. No Image 27Oct
    by
    2018/10/27 Views 3786 

    PHP 확장 모듈을 이용한 C 라이브러리 사용

  3. PHP XML 문서파싱 (SAX 방식 , DOM 방식)

  4. No Image 14Sep
    by
    2018/09/14 Views 3548 

    PHP에서의 대칭 암호화/복호화 ― 간단한 예제에서 DB 입/출력까지

  5. No Image 27Oct
    by
    2018/10/27 Views 3539 

    PHP 문자열에서 검색어를 기준으로 앞뒤로 일정 길이만큼 자르기

  6. No Image 27Oct
    by
    2018/10/27 Views 3536 

    PHP split()와 explode()의 차이점

  7. No Image 27Oct
    by 조쉬
    2018/10/27 Views 3502 

    PHP 소켓을 이용하여 URL의 응답결과를 문자열로 받기

  8. No Image 27Oct
    by
    2018/10/27 Views 3259 

    자바스크립트 이스케이프 문자열을 PHP로 디코딩 하기

  9. No Image 29Aug
    by
    2018/08/29 Views 2694 

    PHP에서 모든 세션 정보를 화면에 출력하는 방법

  10. No Image 29Aug
    by
    2018/08/29 Views 2465 

    PHP에서 자료, 데이터의 타입을 확인하는 방법, gettype()

  11. No Image 29Aug
    by
    2018/08/29 Views 2429 

    날짜/시간함수 정리

  12. No Image 19Feb
    by
    2019/02/19 Views 2183 

    PHP http 를 https 로 전환(redirect), http->https

  13. No Image 08Jan
    by
    2019/01/08 Views 2000 

    메모장소스

  14. No Image 24Jun
    by
    2019/06/24 Views 1945 

    PHP 외부 XML 파싱 하기

  15. No Image 16Jan
    by
    2019/01/16 Views 1912 

    금액 단위를 만단위부터 표시하는방법

  16. No Image 16Jan
    by
    2019/01/16 Views 1850 

    dddotag - 허용하지 않는 태그 걸러내기

  17. No Image 08Jan
    by
    2019/01/08 Views 1808 

    php에서 체크박스 선택한 것 보여주기

  18. No Image 08Jan
    by
    2019/01/08 Views 1631 

    이미지 사이즈 비율로 조정하기

  19. No Image 08Jan
    by
    2019/01/08 Views 1625 

    공백문자 체크

  20. No Image 08Jan
    by
    2019/01/08 Views 1625 

    배열을 테이블로 만들기

Board Pagination Prev 1 ... 6 7 8 9 10 11 12 13 14 15 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved