Setting Timeout Value for file_get_contents in PHP

By | May 20, 2015

Sometimes you need to avoid using cURL for getting external web content for any reasons. You’re lucky that PHP has some built-in functions that allow you to get data from the Web without cURL. But if you are using default values, there is no option to set the timeout for a operation and it’s sometimes annoying. In order to have timeouts enabled, you can use the following code:

<?
$ctx = stream_context_create(array(‘http’ => array(‘timeout’ => 5)));
$checkop=file_get_contents($url, 0, $ctx);
?>

Of course you can change the timeout value.

Leave a Reply

Your email address will not be published. Required fields are marked *