Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

617
Views
Función php get_headers(): ¿hay alguna forma de decirle a esta función que no siga los redireccionamientos?

Función php get_headers(): ¿hay alguna forma de decirle a esta función que no siga los redireccionamientos?

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

respuesta:

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe configurar la opción de contexto max_redirects en 1 o simplemente deshabilitar follow_location :

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

Referencia :

follow_location int

Siga los redireccionamientos del encabezado de ubicación. Establecer en 0 para deshabilitar.

El valor predeterminado es 1.

max_redirects int

El número máximo de redireccionamientos a seguir. El valor 1 o menos significa que no se siguen redireccionamientos.

El valor predeterminado es 20.

over 4 years ago · Santiago Trujillo Report

0

No parece que get_headers() admita. Aquí hay una manera de hacerlo con curl en su lugar:

 <?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'));

Salidas:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!