Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Forked from sabarasaba/gist:1387550
Created October 24, 2024 20:23
Show Gist options
  • Save fhferreira/06209e053c3e3bddd1599fe369e8498c to your computer and use it in GitHub Desktop.
Save fhferreira/06209e053c3e3bddd1599fe369e8498c 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 !
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment