I have a script that I need to be able to detect 404 pages.
I've been using file-get-contents(url) but it returns an empty string on any page that has a redirect - rather than follow the redirect and return the code of the destination page.
In a sense, I can use the empty string returned as an indicator of a 404 page, but it isn't precise, and returns many false positives.
Am I missing something, or am I just using the wrong function?
Martin.
404 detection
Moderator: General Moderators
- ambivalent
- Forum Contributor
- Posts: 173
- Joined: Thu Apr 14, 2005 8:58 pm
- Location: Toronto, ON
I didn't explain very well - sleep deprivation will do that to you!
My script takes a list of urls and then tries to check each one individually to see if it is a 404 page.
That's where it falls down because php file_get_contents is reading the source code of the target url and not the 404 page that is being redirected to.
What I think I need is some way to detect if a target url is redirecting and then read the source of the page it is redirecting to - to check if it is a 404 or not.
But the problem is that when file_get_contents places the source code of a page that redirects into a string, that string returns empty.
Martin.
My script takes a list of urls and then tries to check each one individually to see if it is a 404 page.
That's where it falls down because php file_get_contents is reading the source code of the target url and not the 404 page that is being redirected to.
What I think I need is some way to detect if a target url is redirecting and then read the source of the page it is redirecting to - to check if it is a 404 or not.
But the problem is that when file_get_contents places the source code of a page that redirects into a string, that string returns empty.
Martin.
requires php 4.3+
or http://pear.php.net/manual/en/package.h ... equest.php
Code: Select all
<?php
$fp = fopen('http://www.php.net', 'r');
$meta = stream_get_meta_data($fp);
print_r($meta);
fclose($fp);Code: Select all
<?php
require_once 'HTTP/Request.php';
$request =new HTTP_Request('http://www.php.net');
$err = $request->sendRequest();
if (PEAR::isError($err)) {
echo 'error:', $err->getMessage();
}
else {
echo 'Code: ', $request->getResponseCode(), "<br />\n",
htmlentities($request->getResponseBody());
}
?>