PHP 버전
5+
$url = 'http://example.com'; // 검사할 URL을 변수에 할당합니다.
$headers = get_headers($url); // get_headers() 함수를 호출하여 해당 URL의 모든 응답 헤더를 배열로 가져옵니다.

/*
 * $headers[0]는 응답의 첫 번째 줄(상태 코드)인 'HTTP/1.1 200 OK'를 가리킵니다.
 * strpos() 함수를 이용해 이 문자열 안에 '200'이 포함되어 있는지 확인합니다.
 * 200은 성공적인 응답을 의미하는 상태 코드입니다.
**/
if (strpos($headers[0], '200') !== false) { // 200 코드가 발견되면 URL이 정상적으로 작동한다고 출력합니다.
    echo 'URL이 유효합니다.';
} else { // 200 코드가 없으면 접근할 수 없다고 출력합니다.
    echo 'URL에 접근할 수 없습니다.';
}
get_headers(string $url, bool $associative = false, ?resource $context = null): array|false
$url = 'http://www.example.com';

// $associative를 기본값인 false로 설정: 인덱스 배열 반환
print_r(get_headers($url));

// $associative를 true로 설정: 문자열 타입의 헤더 이름을 키로 하는 연관 배열 반환
print_r(get_headers($url, true));
출력
$headers = get_headers('https://example.com', true);

if (isset($headers['Location'])) {
    echo "리다이렉션 URL: " . $headers['Location'];
}
$headers = get_headers('https://example.com/file.pdf', true);

if (isset($headers['Content-Length'])) {
    echo "파일 크기: " . $headers['Content-Length'] . " 바이트";
}
$headers = get_headers('https://example.com', true);

echo "컨텐츠 타입: " . $headers['Content-Type'];