Методы проверки заголовков в PHP: get_headers(), curl,stream_context_create()

Для проверки заголовков в PHP можно использовать различные методы. Вот несколько часто используемых подходов:

  1. Использование функции get_headers():

    $url = 'http://example.com';
    $headers = get_headers($url);
    // Displaying the headers
    foreach ($headers as $header) {
       echo $header . "<br>";
    }
  2. Использование расширения curl:

    $url = 'http://example.com';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
    // Displaying the headers
    echo $headers;
    curl_close($ch);
  3. Использование функции stream_context_create():

    $url = 'http://example.com';
    $options = [
       'http' => [
           'method' => 'HEAD'
       ]
    ];
    $context = stream_context_create($options);
    $headers = get_headers($url, 0, $context);
    // Displaying the headers
    foreach ($headers as $header) {
       echo $header . "<br>";
    }

Эти методы позволяют получать и отображать заголовки заданного URL-адреса в PHP.