Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

619
Visualizações
php get_headers() Function - is there any way to tell this function not to follow redirects?

php get_headers() Function - is there any way to tell this function not to follow redirects?

$headers = get_headers("http://example.com/test", 1);
var_dump($headers);

response:

https://i.imgur.com/zjv2G8F.png

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You need to set the max_redirects context option to 1 or just disable follow_location:

$context = stream_context_create(
    [
        'http' => [
            'follow_location' => 0,
        ],
    ]
);
$headers = get_headers("http://example.com/test", true, $context);
var_dump($headers);

Reference:

follow_location int

Follow Location header redirects. Set to 0 to disable.

Defaults to 1.

max_redirects int

The max number of redirects to follow. Value 1 or less means that no redirects are followed.

Defaults to 20.

over 4 years ago · Santiago Trujillo Relatório

0

It doesn't look like get_headers() supports that. Here's a way to do with curl instead:

<?php

function getHeadersDontFollowRedirects($url) {
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_HEADER => 1,
        CURLOPT_FOLLOWLOCATION => false,
    ));

    $response = curl_exec($curl);
    curl_close($curl);

    $headers = explode("\n", $response);

    $headers = array_reduce($headers, function($carry, $item) {
        $pieces = explode(":", $item);
        if (count($pieces) === 1) {
            $carry[] = $item;
        } else {
            $carry[$pieces[0]] = $pieces[1];
        }
        return $carry;
    }, []);

    $headers = array_map('trim', $headers);
    $headers = array_filter($headers);

    return $headers;
}

var_dump(getHeadersDontFollowRedirects('http://localhost:3000/redirect.php'));

Outputs:

array(7) {
  [0]=>
  string(18) "HTTP/1.1 302 Found"
  ["Host"]=>
  string(9) "localhost"
  ["Date"]=>
  string(19) "Sat, 10 Jul 2021 00"
  ["Connection"]=>
  string(5) "close"
  ["X-Powered-By"]=>
  string(9) "PHP/8.0.0"
  ["Location"]=>
  string(11) "/target.php"
  ["Content-type"]=>
  string(24) "text/html; charset=UTF-8"
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda