Home >Backend Development >PHP Tutorial >Does `file_get_contents()` have a Timeout Mechanism?

Does `file_get_contents()` have a Timeout Mechanism?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-14 21:40:02516browse

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 Setting:
ini_set('default_socket_timeout', 900); // 900 Seconds (15 Minutes)
  • Stream Context:
$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn