Reputation: 11985
Here's some sample code:
<?php
$fp = fsockopen($host, $port, $errno, $errstr, $connectTimeout);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
echo "connected\n";
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
I've seen
stream_set_timeout($fp, 5);
and
socket_set_option($fp, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>5, "usec"=>0));
,
but the read never times out.
I've seen several caveats in the PHP docs for stream_set_timeout()
:
This function doesn't work with advanced operations like stream_socket_recvfrom(), use stream_select() with timeout parameter instead.
I'd rather not use select()
or a loop. What is the canonical way to have a blocking read with timeout?
Upvotes: 5
Views: 922