Skip to content

Instantly share code, notes, and snippets.

@sabarasaba
Created November 23, 2011 00:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sabarasaba/1387550 to your computer and use it in GitHub Desktop.
Save sabarasaba/1387550 to your computer and use it in GitHub Desktop.
Setting a timeout for the file_get_contents function
<?php
// Create the stream context
$context = stream_context_create(array(
'http' => array(
'timeout' => 3 // Timeout in seconds
)
));
// Fetch the URL's contents
$contents = file_get_contents('http://reddit.com', 0, $context);
// Check for empties
if (!empty($contents))
{
// Woohoo
echo $contents;
}
else
{
// DANG !
}
?>
@topforce
Copy link

topforce commented Nov 9, 2018

Thank you ,it's very helpful .

@hayden-t
Copy link

hayden-t commented Jul 7, 2021

thanks also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment