PHP: file_get_contents timeout.

This is a short guide on how to set the timeout for PHP’s file_get_contents function. In this post, I will show you how to change this timeout setting on both a default and per-request basis.

Changing the default timeout setting.

If you would like to modify the default timeout setting for ALL file_get_contents requests, then you will need change the default_socket_timeout configuration setting in your php.ini file. This value represents the timeout in seconds for all socket-based streams.

The default value of default_socket_timeout is set to 60 seconds. As a result, HTTP requests will fail if they are not completed within that time frame.

To find out what your current default value is, you can use the init_get function like so:

//Retrieve and print out the value of default_socket_timeout.
echo ini_get('default_socket_timeout');

In my case, the above piece of code printed out the integer 60. i.e. 60 seconds.

To change this setting, you can modify the directive in your php.ini file. Below is an example of the timeout value being set to 120 seconds:

default_socket_timeout = 120

If you are unable or unwilling to change your php.ini file, then you can use the init_set function, which allows you to set configuration values on the fly:

//Set default_socket_timeout to 180 seconds.
ini_set('default_socket_timeout', 180);

In the above example, I used the ini_set function to change default_socket_timeout to 180 seconds. i.e. 3 minutes.

Per-request.

If you would like to increase the timeout for one particular HTTP request, then you can use PHP’s stream_context_create function like so:

//Create a custom stream context that has a HTTP timeout
//of 120 seconds.
$streamContext = stream_context_create(
    array('http'=>
        array(
            'timeout' => 120,  //120 seconds
        )
    )
);

//Pass this stream context to file_get_contents via the third parameter.
$result = file_get_contents('http://localhost/example/', false, $streamContext);

In the code above, we created a custom stream context using the stream_context_create function. This stream context has a HTTP timeout of 120 seconds. We then passed this stream context in as the third parameter to file_get_contents.

Warning about script timeouts.

You will also have to be wary of the fact that PHP scripts have a maximum execution time. i.e. If your request takes 180 seconds to complete, but your maximum execution time is set to 120, then the PHP script will throw a fatal error. To avoid this, you will also have to use the set_time_limit function:

//Set max execution time of the script to 190 seconds.
set_time_limit(190);

In the snippet above, we set the maximum execution time to 190 seconds.

Conclusion.

I would recommend that you use the stream context method, as increasing the timeout for ALL requests made via file_get_contents is a tad bit overkill. The main question to ask yourself here is this: Should all of my HTTP requests be given more than 60 seconds to complete?

In my opinion, the answer is no.