stream_set_timeout()

stream_set_timeout() — Set timeout period on a stream.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Sets the timeout value on stream, expressed in the sum of seconds and microseconds.

Signature

stream_set_timeout(resource $stream, int $seconds, int $microseconds = 0): bool

Parameters

ParameterTypeWhat it takes
$streamresourceThe target stream.
$secondsintThe seconds part of the timeout to be set.
$microsecondsintThe microseconds part of the timeout to be set.

Return value

Returns true on success or false on failure.

Example

A runnable snippet:

$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; } }

See also

fsockopen(), fopen()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page stream_set_timeout() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.