Last active
March 1, 2021 16:17
-
-
Save vyspiansky/5a1840c728caacc960a7dfab1f9a9f76 to your computer and use it in GitHub Desktop.
PHP: set get_headers timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/PHP-logo.svg/100px-PHP-logo.svg.png'; | |
// Store previous default context | |
$prev = stream_context_get_options(stream_context_get_default()); | |
// Set a small timeout | |
stream_context_set_default([ | |
'http' => [ | |
'timeout' => 3, // seconds | |
] | |
]); | |
// Do the head request | |
$req = @get_headers($url, true); | |
if (!empty($req)) { | |
// Do some useful stuff here | |
print_r($req); | |
} | |
// Restore previous default context | |
stream_context_set_default($prev); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment