Home >Backend Development >PHP Tutorial >Does `file_get_contents()` have a Timeout Mechanism?
Does File_Get_Contents() Timeout?
File_get_contents() is a powerful tool for retrieving content from remote URLs. However, concerns arise regarding potential timeouts when dealing with lengthy processes.
Does File_Get_Contents() Have a Timeout?
Fortunately, file_get_contents() does have a timeout mechanism. By default, the timeout is set by the default_socket_timeout ini-setting, which is configured to 60 seconds. This means that if the content cannot be retrieved within 60 seconds, a timeout exception will be thrown.
Customizing the Timeout
The default timeout can be customized to suit specific needs. Two methods can be used:
ini_set('default_socket_timeout', 900); // 900 Seconds (15 Minutes)
$ctx = stream_context_create(array( 'http' => array( 'timeout' => 1200 // 1200 Seconds (20 Minutes) ) )); echo file_get_contents('http://example.com/', false, $ctx);
By setting a higher timeout, users can ensure that file_get_contents() waits longer before triggering a timeout.
The above is the detailed content of Does `file_get_contents()` have a Timeout Mechanism?. For more information, please follow other related articles on the PHP Chinese website!