개발자 Q&A

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

2025.06.29 06:35

stream_context_set_options 함수 사용법에 대한 질문

목록
  • 패턴마스터 17일 전 2025.06.29 06:35 인기
  • 134
    1
제가 stream_context_set_options 함수를 사용하여 HTTP 요청을 보낼 때,
stream_context_set_options 함수의 첫 번째 인자로 전달하는 context 변수에 대해 궁금합니다.
context 변수의 타입은 무엇이고, 이 변수에 어떤 값을 넣어야 하는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  17일 전



    stream_context_set_options 함수의 첫 번째 인자로 전달하는 context 변수의 타입은 stream_context입니다.

    stream_context는 HTTP 요청을 보낼 때 사용하는 옵션을 설정하는 변수입니다.

    context 변수에 값을 넣는 방법은 두 가지가 있습니다.

    첫 번째 방법은 stream_context_create 함수를 사용하여 stream_context 변수를 생성하고, 그 변수에 옵션을 설정하는 것입니다.

    예를 들어, 다음 코드는 stream_context_create 함수를 사용하여 stream_context 변수를 생성하고, 그 변수에 follow_location 옵션을 설정하는 코드입니다.

    #hostingforum.kr
    php
    
    $opts = array(
    
        'http' => array(
    
            'method'  => 'GET',
    
            'follow_location' => 0
    
        )
    
    );
    
    
    
    $context  = stream_context_create($opts);
    
    


    두 번째 방법은 stream_context_set_option 함수를 사용하여 stream_context 변수에 옵션을 설정하는 것입니다.

    예를 들어, 다음 코드는 stream_context_set_option 함수를 사용하여 follow_location 옵션을 설정하는 코드입니다.

    #hostingforum.kr
    php
    
    $opts = stream_context_get_options($context);
    
    $opts['http']['follow_location'] = 0;
    
    stream_context_set_options($context, $opts);
    
    


    context 변수에 값을 넣는 방법은 위의 두 가지 방법 중 하나를 사용하면 됩니다.

    context 변수에 값을 넣은 후, stream_context_set_options 함수를 사용하여 옵션을 설정할 수 있습니다.

    예를 들어, 다음 코드는 context 변수에 follow_location 옵션을 설정하는 코드입니다.

    #hostingforum.kr
    php
    
    stream_context_set_options($context, array(
    
        'http' => array(
    
            'follow_location' => 0
    
        )
    
    ));
    
    


    context 변수에 값을 넣은 후, stream_context_set_options 함수를 사용하여 옵션을 설정할 수 있습니다.

    stream_context_set_options 함수는 context 변수에 설정된 옵션을 사용하여 HTTP 요청을 보냅니다.

    예를 들어, 다음 코드는 stream_context_set_options 함수를 사용하여 HTTP 요청을 보내는 코드입니다.

    #hostingforum.kr
    php
    
    $opts = array(
    
        'http' => array(
    
            'method'  => 'GET',
    
            'follow_location' => 0
    
        )
    
    );
    
    
    
    $context  = stream_context_create($opts);
    
    $html = file_get_contents('http://example.com', false, $context);
    
    

    2025-06-29 06:36

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

검색

게시물 검색