|
Posted by Mario Micklisch on 03/05/05 00:56
[..]
> into a variable. I need to include some sort of error checking that
> will kill this request if for some reason the URL request hangs for
> more then 15 seconds. In researching this, I think the correct
> function to use is fsockopen, but I can't seem to get it to work. Can
> someone verify if fsockopen is the best way to grab an external URL,
> but kill the request if it hangs for a certain amount of time ... and
> if so, show me some sample code on how to place this URL's contents
> into a variable that I can then parse. The URL will be hard coded into
[..]
stream_set_timeout should be what you're looking for.
might look like this:
<?php
$fp = fsockopen("www.example.com", 80);
if (!$fp) {
echo "Unable to open\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
stream_set_timeout($fp, 2);
$res = fread($fp, 2000);
$info = stream_get_meta_data($fp);
fclose($fp);
if ($info['timed_out']) {
echo 'Connection timed out!';
} else {
echo $res;
}
}
?>
-- from the manual:
http://us4.php.net/manual/en/function.stream-set-timeout.php
$res will hold the first 2,000 bytes of the result.
socket blocking would be another way, but the above one looks like exactly
what you're looking for
[Back to original message]
|