Re: how do i get url: response codes?
| From: | infoz | Date: | Thu, 12 Jul 2001 13:12:40 +0000 |
| Subject: | Re: how do i get url: response codes? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-57584@lists.php.net to get a copy of this message | ||
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" <patrick@arch.ethz.ch>
To: <php-general@lists.php.net>
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...)