Re: Finding out which file is retrieved over HTTP

From: Date: Tue, 25 Mar 2003 19:48:01 +0000
Subject: Re: Finding out which file is retrieved over HTTP
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-140996@lists.php.net to get a copy of this message
Ernest E Vogelsinger wrote:
At 13:08 25.03.2003, Jens Lehmann spoke out and said: --------------------[snip]--------------------
A thing which seemed a bit confusing to me is that if I open a non-existing website (fopen('http://www.amazon.de/nonsensestuff')) I always get an error message "Success". It's the same thing if I use file(). The manual explains that fopen() returns false if the website could not be opened, that's why I don't know why this error message appears? Besides this "Success"-message is surely a very bad error message. I use PHP 4.2.3.
--------------------[snip]-------------------- If the website doesn't exist, but the web SERVER does, it will return something, usually a 404-message. fopen() doesn't check for HTTP result codes, it just opens the stream and retuirns whatever gets sent (with the single exception of omitting the headers, but without looking).
AFAIR fopen() doesn't return a valid file-handle in case of a 404-Error, but prints this error-message.
So in fact an fopen() to a non-existing page will be seen as successful by fopen(). This is by design and afaik documented somewhere.
How can I avoid the error-message, except putting an "@" before it? fopen() should imho either return a file-handle for the 404-Error-message-file or better return false.
To actually check on the HTTP status codes you need to run your own, either using cURL, or by doing your own stuff using fsockopen().
I tried using fsockopen(), but still experience a problem, I want to use the following script to retrieve both, the headers and the actual file: $hostname = 'www.example.com'; $file = '/index.php'; $ip = gethostbyname($hostname); $fp = fsockopen($ip, 80, &$errno, &$errstr); if ($fp) {
    fputs( $fp, "GET ".$file." HTTP/1.0\n\n" );
    $src = '';
    while (!feof ($fp))
        $src .= fgets($fp, 4096);
    fclose ($fp);
} else [...] However I don't get http://www.example.com/index.php, but the page of the provider. I assume this has something to do with virtual IPs, but I don't really know how to get around this. It doesn't work to put the hostname directly in fsockopen(), too. Jens

« previous php.general (#140996) next »