I believe what you are looking for stream_set_timeout. Example:
stream_set_timeout($fp, 2);
fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
You can check if a timeout occurs by checking the flow metadata:
$info = stream_get_meta_data($fp);
// $info['timed_out'] == true : time-out has happened
source
share