Skip to end of metadata
Go to start of metadata

To set timeout in get_headers(), you will need to set stream_context_set_default(), so  you can optimize communication cost as you designed.Below is an example to call get_headers() based on stream_context in the system.


$url = '';

// Store previous default context
$prev = stream_context_get_options(stream_context_get_default());

// Set a small timeout
    'http' => [
        'timeout' => 3, // seconds

// Do the head request
$req = @get_headers($url, true);

if (!empty($req)) {
    // Do some useful stuff here

// Restore previous default context


Original source: