Here's some code I've been using for that. YMMV. :)
- Tim
http://www.phptemplates.org
//
// checks $url, returns true/false and sets the response code in $response
//
function dt_checklink($url, &$response) {
$ua = parse_url($url);
$host = $ua[host];
$port = ($ua[port] != 0) ? $ua[port] : 80;
$path = ($ua[path] != "") ? $ua[path] : "/";
$http_req = "HEAD $path HTTP/1.1\r\nHOST: $host\r\nConnection:
close\r\n\r\n";
$http_resp = "";
$response = "999";
$fp = @fsockopen($host, 80);
if ($fp) {
socket_set_timeout($fp, 20);
$start = time();
fputs($fp, $http_req);
while (!feof($fp) && ((time()-$start)<20)) {
$http_resp .= fgets($fp, 256);
}
fclose($fp);
if (ereg("HTTP/[0-9]\.[0-9] ([0-9][0-9][0-9]) ", $http_resp, $regs)) {
$rcode = $regs[1];
$response = $rcode;
if ($rcode == 200) return true; // OK
if ($rcode == 301) return true; // document moved
if ($rcode == 302) return true; // document moved
return false;
}
return false;
}
return false;
}
----- Original Message -----
From: "Patrick Sibenaler" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, July 12, 2001 6:28 AM
Subject: [PHP] how do i get url: response codes?
> I've tried for a while now to figure out how to test from within php
> if a url (http://xxx/file.html) is present and what error code is
> returned (200,201,202,404, etc...)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]