note 102050 added to function.get-headers
| From: | domwatson at live dot co dot uk | Date: | Tue, 25 Jan 2011 09:56:18 +0000 |
| Subject: | note 102050 added to function.get-headers | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176277@lists.php.net to get a copy of this message | ||
Was looking for something that checks a website to see if its OK but couldn't find a nice and
cheap solution. Looking through what Weboide and Nick said, I've combined the two into a
function.
The function below will return a response code and allow you to follow redirections as well to
handle those 301/302s
function get_http_response_code($url, $followRedirects = true) {
# Supress errors due to warnings on failure
$headers = @get_headers($url);
$responseCode = substr($headers[0], 9, 3);
if ($followRedirects){
foreach($headers as $header){
if ($header === 'HTTP/1.1 200 OK'){
$responseCode = '200';
break 1;
}
}
}
return $responseCode;
}
# Follow redirection
$code = get_http_response_code('http://www.konoro.co.uk/', true);
// string '200' (length=3)
var_dump($code);
# Do not follow redirection, get first response code
$code = get_http_response_code('http://www.konoro.co.uk/', false);
// string '301' (length=3)
var_dump($code);
Cheers, Dom
----
Server IP: 69.147.83.197
Probable Submitter: 80.85.101.154
----
Manual Page -- http://www.php.net/manual/en/function.get-headers.php
Edit -- https://master.php.net/note/edit/102050
Del: integrated -- https://master.php.net/note/delete/102050/integrated
Del: useless -- https://master.php.net/note/delete/102050/useless
Del: bad code -- https://master.php.net/note/delete/102050/bad+code
Del: spam -- https://master.php.net/note/delete/102050/spam
Del: non-english -- https://master.php.net/note/delete/102050/non-english
Del: in docs -- https://master.php.net/note/delete/102050/in+docs
Del: other reasons-- https://master.php.net/note/delete/102050
Reject -- https://master.php.net/note/reject/102050
Search -- https://master.php.net/manage/user-notes.php