HTTP response code after redirect

There is a redirect to the server for information, and as soon as the response comes from the server, I want to check the HTTP code to throw an exception if there is code starting with 4XX. To do this, I need to know how I can only get the HTTP code from the header? It is also redirected to the server here, so I'm afraid that curl will not be useful to me.

So far I have tried this solution , but it is very slow and creates a script time in my case. I do not want to increase the waiting period of the script and wait longer to get the HTTP code.

Thanks in advance for any suggestion.

+6
source share
3 answers

Your method with get_headers and requesting the first line of response will return a redirection status code (if any), and, more importantly, it will execute a GET request that will transfer the entire file.

You only need a HEAD request and then parse the headers and return the last status code. The following is sample code that does this using $http_response_header instead of get_headers , but the array format is the same:

 $url = 'http://example.com/'; $options['http'] = array( 'method' => "HEAD", 'ignore_errors' => 1, ); $context = stream_context_create($options); $body = file_get_contents($url, NULL, $context); $responses = parse_http_response_header($http_response_header); $code = $responses[0]['status']['code']; // last status code echo "Status code (after all redirects): $code<br>\n"; $number = count($responses); $redirects = $number - 1; echo "Number of responses: $number ($redirects Redirect(s))<br>\n"; if ($redirects) { $from = $url; foreach (array_reverse($responses) as $response) { if (!isset($response['fields']['LOCATION'])) break; $location = $response['fields']['LOCATION']; $code = $response['status']['code']; echo " * $from -- $code --> $location<br>\n"; $from = $location; } echo "<br>\n"; } /** * parse_http_response_header * * @param array $headers as in $http_response_header * @return array status and headers grouped by response, last first */ function parse_http_response_header(array $headers) { $responses = array(); $buffer = NULL; foreach ($headers as $header) { if ('HTTP/' === substr($header, 0, 5)) { // add buffer on top of all responses if ($buffer) array_unshift($responses, $buffer); $buffer = array(); list($version, $code, $phrase) = explode(' ', $header, 3) + array('', FALSE, ''); $buffer['status'] = array( 'line' => $header, 'version' => $version, 'code' => (int) $code, 'phrase' => $phrase ); $fields = &$buffer['fields']; $fields = array(); continue; } list($name, $value) = explode(': ', $header, 2) + array('', ''); // header-names are case insensitive $name = strtoupper($name); // values of multiple fields with the same name are normalized into // a comma separated list (HTTP/1.0+1.1) if (isset($fields[$name])) { $value = $fields[$name].','.$value; } $fields[$name] = $value; } unset($fields); // remove reference array_unshift($responses, $buffer); return $responses; } 

For more information, see HEAD first with PHP streams , at the end it contains sample code on how you can execute a HEAD request with get_headers .

Related: How to check if a remote file exists using PHP?

+8
source

Sort of:

  $ch = curl_init(); $httpcode = curl_getinfo ($ch, CURLINFO_HTTP_CODE ); 

You should try the HttpEngine class. Hope this helps.

-

EDIT

 $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $your_agent_variable); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, $your_referer); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $output = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode ...) 
+6
source

The solution you found looks good. If the server cannot send you the http headers on time, your problem is that the other server is damaged or is under very heavy load.

0
source

Source: https://habr.com/ru/post/898132/


All Articles