Reputation: 307
I've been using the get_meta_tags() & get_headers() PHP functions, and need to set a timeout value in case the website is slow or unresponsive. Does anyone know how to do it?
Upvotes: 15
Views: 14569
Reputation: 11
@redanimalwar, how about getting the default options and setting it back for to the context, something like this:
$opts['http']['timeout'] = 2;
$headers = null;
if (version_compare(PHP_VERSION, '7.1.0', '>=')) {
$context = stream_context_create($opts);
$headers = @get_headers($url, 0, $context);
} else {
$defaultOptions = stream_context_get_options(stream_context_get_default());
stream_context_set_default($opts);
$headers = @get_headers($url);
stream_context_set_default($defaultOptions);
}
return $headers;
Upvotes: 0
Reputation: 1523
Doing this as posted by @Gordon just with get_headers but stream_context_set_default
returns ressource
and not a array so I am not sure how I am supposed to feed that back into the same function. It expect a array.
$originalDefaults = stream_context_set_default( … );
$meta = get_meta_tags( … );
stream_context_set_default($originalDefaults);
var_dump($http_response_header);
In php 7.1 there was a 3rd parameter added to get_headers
. So I came up with this. When on a not outdated PHP version the option is added to the get headers call only, otherwise the defaults will stick for the rest of the script execution until someone explains to me how to do it on older php versions.
function ngt_headers( $url ) {
$opts['http']['timeout'] = 2;
if ( version_compare(PHP_VERSION, '7.1.0', '>=') ) {
$context = stream_context_create( $opts );
return @get_headers( $url, 0, $context ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
} else {
stream_context_set_default( $opts );
return @get_headers( $url ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
}
}
Upvotes: 0
Reputation: 317177
The get_headers
and get_meta_tags
function use the default HTTP Stream Wrapper underneath. You can either change the ini setting as shown elsewhere on this page or modify the behavior of that wrapper and set a specific timeout:
stream_context_set_default(
array(
'http' => array(
'timeout' => 5
)
)
);
Note that changing the default HTTP Stream Context will apply to all functions using it. If you want to restore the timeout to the original default settings, do:
$originalDefaults = stream_context_set_default( … );
$meta = get_meta_tags( … );
stream_context_set_default($originalDefaults);
On a sidenote, if you call any functions using an HTTP Stream Wrapper, PHP will also automatically populate the variable $http_response_header
in the current scope, so you don't have to call get_headers
in addition, e.g.
$originalDefaults = stream_context_set_default( … );
$meta = get_meta_tags( … );
stream_context_set_default($originalDefaults);
var_dump($http_response_header);
Upvotes: 8
Reputation: 14159
You should be able to influence this (as it's via URL wrappers) with the default_socket_timeout
ini setting.
Try either setting it in the php.ini
file or by doing something like
ini_set('default_socket_timeout', 10);
to set a 10 sec timeout (the default value is 60)
Upvotes: 20