G
G
Gorthaur2015-10-10 15:11:00
PHP
Gorthaur, 2015-10-10 15:11:00

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";
}
?>

Necessary to monitor pages for http availability, because ping does not always give correct information (ICMP is closed on some machines, some ping but the web server is down)
UPD
It worked! Working code for information:
<?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

2 answer(s)
R
Roman Mirilaczvili, 2015-10-10
@gorthaur

I don't work with PHP, but I think you should use cURL and CURLOPT_CONNECTTIMEOUT.

O
Orzubek Rakhimov, 2015-10-10
@orzubek

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 question

Ask a Question

731 491 924 answers to any question