multipart-message nevyn bengtsson's blog

featured articles 🦄, about, archive, tags

PHP: Timeout on file_get_contents

Since there doesn’t seem to be a single piece of documentation or example on the use of the context option for file_get_contents, and everyone’s actually setting the PHP app’s ini value for timeout (euugh) instead of doing it right, I thought I’d feed this to google:

$ctx = stream_context_create(array(
    ‘http’ => array(
        'timeout’ => 1
        )
    )
);
file_get_contents(“http://google.com/”, 0, $ctx);

The unit on the timeout argument is seconds as a float; that is, it is possible to use fractions (e g set timeout to 0.1).

Tagged php, coding, webdev