Home > Backend Development > PHP Tutorial > Can I Increase the Timeout in file_get_contents()?

Can I Increase the Timeout in file_get_contents()?

DDD
Release: 2024-11-17 09:31:04
Original
577 people have browsed it

Can I Increase the Timeout in file_get_contents()?

Timeouts in file_get_contents()

This query addresses concerns regarding timeouts when employing file_get_contents() to fetch data from a series of links within a loop, where each link could take significantly longer than 15 minutes to process.

Does file_get_contents() Have a Timeout Setting?

Yes, file_get_contents() has a default timeout as specified by the default_socket_timeout PHP configuration option. The default value for this option is 60 seconds (1 minute).

How to Adjust the Timeout Period?

You can modify the timeout period using any of these methods:

  • Using ini_set:
ini_set('default_socket_timeout', 900); // 900 seconds (15 minutes)
Copy after login
  • Using stream_context_create:
$ctx = stream_context_create([
    'http' => [
        'timeout' => 1200, // 1200 seconds (20 minutes)
    ]
]);

echo file_get_contents('http://example.com/', false, $ctx);
Copy after login

Note: It's important to note that these timeout settings apply to the connection and communication process, not to the file processing time of the remote server.

The above is the detailed content of Can I Increase the Timeout in file_get_contents()?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template