개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.05.20 07:11

SwooleHttpResponse::write 메서드 사용하기

목록
  • 프로그래밍도깨비 10일 전 2025.05.20 07:11
  • 22
    1
저는 현재 Swoole 프레임워크를 사용하여 웹 서버를 개발 중입니다.
SwooleHttpResponse::write 메서드를 사용하여 HTTP 응답을 보낼 때,
해당 메서드는 반드시 HTTP 응답 헤더를 전부 보낸 후에만 호출할 수 있는 것인 것 같습니다.
하지만, 이 메서드는 HTTP 헤더를 별도로 전달할 수 있는 방법을 제공하지 않는 것 같습니다.
이러한 경우 HTTP 헤더를 별도로 전달할 수 있는 방법은 없을까요?

예를 들어, HTTP 헤더를 다음과 같이 전달하고 싶은데요,
HTTP 헤더를 별도로 전달할 수 있는 방법은 없을까요?

php

$response = new SwooleHttpResponse();

$response->write("HTTP/1.1 200 OKrn");

$response->write("Content-Type: text/htmlrn");

$response->write("rn");

$response->write("...");



위와 같이 HTTP 헤더를 별도로 전달하고 싶은데요,
별도로 전달할 수 있는 방법은 없을까요?

    댓글목록

    profile_image
    나우호스팅  10일 전



    SwooleHttpResponse::write 메서드는 HTTP 응답 헤더를 전부 보낸 후에만 호출할 수 있습니다.

    HTTP 헤더를 별도로 전달하고 싶다면, SwooleHttpResponse::write 메서드를 사용하기 전에 HTTP 헤더를 모두 작성한 후에 호출할 수 있습니다.

    예를 들어, 다음 코드를 사용할 수 있습니다.

    #hostingforum.kr
    php
    
    $response = new SwooleHttpResponse();
    
    $response->write("HTTP/1.1 200 OKrn");
    
    $response->write("Content-Type: text/htmlrn");
    
    $response->write("rn");
    
    
    
    // HTTP 헤더를 별도로 전달할 수 있습니다.
    
    $response->write("X-Custom-Header: custom-valuern");
    
    
    
    $response->write("...");
    
    


    또는, HTTP 헤더를 별도로 전달할 수 있도록 SwooleHttpResponse 클래스를 상속하여 사용할 수 있습니다.

    #hostingforum.kr
    php
    
    class CustomResponse extends SwooleHttpResponse
    
    {
    
        public function write($data)
    
        {
    
            if (strpos($data, "rnrn") !== false) {
    
                // HTTP 헤더가 끝났습니다.
    
                parent::write($data);
    
            } else {
    
                // HTTP 헤더가 아직 끝나지 않았습니다.
    
                $this->body .= $data;
    
            }
    
        }
    
    }
    
    


    이러한 방법으로 HTTP 헤더를 별도로 전달할 수 있습니다.

    2025-05-20 07:12

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 23,833건 / 2 페이지

검색

게시물 검색