URLs get_headers
(PHP 5, PHP 7, PHP 8)
get_headers — HTTP 요청에 대한 응답으로 서버에서 보낸 모든 헤더를 가져옵니다.
설명
get_headers(string $url
, bool $associative
= false
, ?resource $context
= null
): array|false
get_headers()는 HTTP 요청에 대한 응답으로 서버에서 보낸 헤더가 있는 배열을 반환합니다.
매개변수
url
- 대상 URL입니다.
associative
- 선택적
associative
매개변수가 true로 설정되면 get_headers()가 응답을 구문 분석하고 배열의 키를 설정합니다. context
- stream_context_create()로 생성된 유효한 컨텍스트 리소스 또는 기본 컨텍스트를 사용하려면
null
입니다.
반환 값
헤더가 있는 인덱스 또는 연관 배열을 반환하거나 실패 시 false
를 반환합니다.
변경 로그
버전 | 설명 |
---|---|
8.0.0 | associative 이 int에서 bool로 변경되었습니다. |
7.1.0 | context 매개변수가 추가되었습니다. |
Examples
예제 #1 get_headers() 예제
<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, true));
?>
위의 예는 다음과 유사한 결과를 출력합니다.
Array ( [0] => HTTP/1.1 200 OK [1] => Date: Sat, 29 May 2004 12:28:13 GMT [2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT [4] => ETag: "3f80f-1b6-3e1cb03b" [5] => Accept-Ranges: bytes [6] => Content-Length: 438 [7] => Connection: close [8] => Content-Type: text/html ) Array ( [0] => HTTP/1.1 200 OK [Date] => Sat, 29 May 2004 12:28:14 GMT [Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux) [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT [ETag] => "3f80f-1b6-3e1cb03b" [Accept-Ranges] => bytes [Content-Length] => 438 [Connection] => close [Content-Type] => text/html )
예제 #2 HEAD 예제를 사용하는 get_headers()
<?php
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers('http://example.com');
?>
기타
- apache_request_headers() - 모든 HTTP 요청 헤더 가져오기