note 117256 modified in function.get-headers by cmb
| From: | cmb@php.net | Date: | Mon, 18 May 2015 11:29:49 +0000 |
| Subject: | note 117256 modified in function.get-headers by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-202588@lists.php.net to get a copy of this message | ||
Testing the validity of a URL that is preceded by one or more server redirects is tricky. There will
be more than one status code returned and all but the first will be redirect codes.
This function will return an integer containing the three digit status code of the last code
returned, which is what you want.
function getStatus($url) {
$headers = @get_headers($url, true);
$value = NULL;
if ($headers === false) {
return $headers;
}
foreach ($headers as $k => $v) {
if (!is_int($k)) {
continue;
}
$value = $v;
}
return (int) substr($value, strpos($value, ' ', 8) + 1, 3);
}
If getHeaders() fails, PHP will throw an error. Test the return value for === false.
--was--
Testing the validity of a URL that is preceded by one or more server redirects is tricky. There will
be more than one status code returned and all but the first will be redirect codes.
This function will return an integer containing the three digit status code of the last code
returned, which is what you want.
function getStatus($url) {
$headers = @get_headers($url, true);
foreach($headers as $k => $v) {
if (! is_int($k)) continue;
$value = $v;
}
return (int) substr($value, strpos($value,' ', 8) +1, 3);
}
http://php.net/manual/en/function.get-headers.php