Timeout Considerations for file_get_contents()
When utilizing file_get_contents() to fetch data from a remote link, it's crucial to consider its timeout implications. By default, file_get_contents() inherits its timeout duration from the PHP ini setting default_socket_timeout, which defaults to 60 seconds. If a retrieval operation exceeds this limit, the request will time out prematurely.
Overriding the Default Timeout
To modify the default timeout setting, there are two primary approaches:
Ini Setting Modification: Utilize ini_set() to adjust the default_socket_timeout value. For example:
ini_set('default_socket_timeout', 900); // 900 Seconds = 15 Minutes
Stream Context Configuration: Define a custom stream context using stream_context_create() and specify the desired timeout as HTTP context options. Here's an example:
$ctx = stream_context_create(array('http' => array('timeout' => 1200))); //1200 Seconds is 20 Minutes echo file_get_contents('http://example.com/', false, $ctx);
Note: Remember that the timeout duration applies to the entire file retrieval process, including network latency and server processing time. Therefore, it's essential to set an appropriate timeout value that accounts for potential delays.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!