|
Posted by NC on 12/11/05 09:39
Csaba Gabor wrote:
>
> I've got a section of code that takes a user supplied $url and
> essentially does:
> function redirectRequest($url) {
> $resultRemote = @file_get_contents($url);
> return $resultRemote; }
>
> Is there a reasonable way to trap for and differentiate between
> the major (most common) types of failure?
Not if you insist on using file_get_contents()...
> For example, common scenarios in which I would not get back a desired
> response include: malformed url, the domain can't be mapped (ie. the
> DNS servers don't return an IP address), the domain is not reachable,
> the directory or file is not found, standard HTTP error response
> header.
Consider an alternative function that does something like this:
1. Parse the argument URL into parts using parse_url() to
verify that it is not obvously malformed.
2. Run the server name through gethostbyname() to verify
that the host name in fact resolves to an IP address.
3. Connect to the server using fsockopen() to verify that
it is operational.
4. Attempt to retrieve the file by emulating a GET request
with fputs() and reading response by fgets().
5. Parse the response headers and, if applicable, the body.
Cheers,
NC
[Back to original message]
|