Function socket_set_timeout is Deprecated in PHP 8.5

Function socket_set_timeout is Deprecated in PHP 8.5

PHP provides the socket_set_timeout function, which has historically been used to define read and write timeouts for stream resources such as network connections. This function is an alias of the stream_set_timeout function and does not provide any additional functionality. The name socket_set_timeout implies that it works exclusively with low-level socket resources - when in reality it operates on streams - it has long been a source of confusion.

Since PHP 8.5, the socket_set_timeout function is deprecated:

<?php

$fp = fsockopen('httpbin.org', 80);
fwrite($fp, "GET /get HTTP/1.1\r\nHost:httpbin.org\r\nConnection:close\r\n\r\n");
socket_set_timeout($fp, 30);

echo stream_get_contents($fp);
fclose($fp);

Example will output:

Deprecated: Function socket_set_timeout() is deprecated since 8.5, use stream_set_timeout() instead ...

To resolve the deprecation warning and align the code with current PHP standards, replace socket_set_timeout with stream_set_timeout. The behavior remains the same, but the function name more clearly reflects its purpose.

<?php

$fp = fsockopen('httpbin.org', 80);
fwrite($fp, "GET /get HTTP/1.1\r\nHost:httpbin.org\r\nConnection:close\r\n\r\n");
stream_set_timeout($fp, 30);

echo stream_get_contents($fp);
fclose($fp);

Leave a Comment

Cancel reply

Your email address will not be published.