Answer the question
In order to leave comments, you need to log in
How to reduce the execution time of a php script when requesting a response from the server?
Good afternoon. There is a code that takes the server response code. When executing it on a working link, it gives the desired answer, when executing on a non-working link, instead of the answer, it falls off by timeout.
how can I force it to give a response if the page does not return anything for 20 seconds, for example?
set_timeout and max_execution_time don't work.
<?php
$url='http://example.com';
$code=get_headers($url);
if ($code[0]="HTTP/1.1 200 OK")
{
echo "200";
} else {
echo "shit";
}
?>
<?php
$url = "http://example.com";
$ch = curl_init( $url );
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_TIMEOUT => 20,
CURLOPT_NOBODY => 1
);
curl_setopt_array( $ch, $options );
curl_exec($ch);
$http = curl_getinfo($ch);
echo $http['http_code'];
curl_close($ch);
?>
Answer the question
In order to leave comments, you need to log in
I don't work with PHP, but I think you should use cURL and CURLOPT_CONNECTTIMEOUT.
A typical question: "How can I reduce the time of day by 3 hours?" No way, you can, if you only shorten the script from the server side, shortening is not an easy task.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question