Re: Testing Hyperlinks on in a database.
| From: | Michael Sims | Date: | Thu, 31 Jan 2002 06:09:56 +0000 |
| Subject: | Re: Testing Hyperlinks on in a database. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-82874@lists.php.net to get a copy of this message | ||
At 03:04 PM 1/31/2002 +1300, Philip J. Newman wrote:
Is there anyway using PHP to send a command to a server to get the get eather a 404 or 200 reply?Here's a function I put together to do just that. It takes a full URL as an argument and returns either boolean true, boolean false, or a string error message. Since it might return a string error you need to check the type returned when using it, such as: <? if($http_file_exists("http://someurl.tld/page.html") === true) { //do stuff } ?> Note the three equals signs that checks for type as well as value. Anyway, here is the function: <? function http_file_exists ($url) { if (!preg_match("/^http:\/\/([^\/]+)\/.*$/i",$url,$matches)) {
return "Error - incorrect format";} else {
$host = $matches[1];
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp) return "Error - couldn't connect to host";
else {
fputs ($fp, "HEAD $url HTTP/1.0\r\n\r\n");
$response = fgets ($fp,128);
fclose ($fp);
return (eregi("^.+200 OK.+$",$response) ? true : false);
}
}
}
?>