Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your method with <code>get_headers</code> and requesting the first response line will return the status code of the redirect (if any) <em>and</em> more importantly, it will do a GET request which will transfer the whole file.</p> <p>You need only a HEAD request and then to parse the headers and return the <em>last</em> status code. Following is a code example that does this, it's using <code>$http_response_header</code> instead of <code>get_headers</code>, but the format of the array is the same:</p> <pre><code>$url = 'http://example.com/'; $options['http'] = array( 'method' =&gt; "HEAD", 'ignore_errors' =&gt; 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&lt;br&gt;\n"; $number = count($responses); $redirects = $number - 1; echo "Number of responses: $number ($redirects Redirect(s))&lt;br&gt;\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 --&gt; $location&lt;br&gt;\n"; $from = $location; } echo "&lt;br&gt;\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' =&gt; $header, 'version' =&gt; $version, 'code' =&gt; (int) $code, 'phrase' =&gt; $phrase ); $fields = &amp;$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; } </code></pre> <p>For more information see: <a href="http://hakre.wordpress.com/2011/09/17/head-first-with-php-streams/" rel="nofollow noreferrer">HEAD first with PHP Streams</a>, at the end it contains example code how you can do the HEAD request with <code>get_headers</code> as well.</p> <p>Related: <a href="https://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php">How can one check to see if a remote file exists using PHP?</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload