M
M
Maxim Morozov2018-01-29 16:46:49
PHP
Maxim Morozov, 2018-01-29 16:46:49

How to catch timeout when using file_get_contents?

It's simple:

$context = stream_context_create([
  'http' => [
    'timeout' => (float) 20
  ]
]);

$result = file_get_contents('http://адрес.ru', false, $context);

How to catch a request that falls off on a timeout? For some reason, the response code for the necessary freezing pages is always 200. File_get_contents does not throw exceptions, only warnings (error_reporting (E_ALL) will not help in this case)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman PHP, 2018-01-29
@murzix

crutches !))
curl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question