HTTP Response Code 0 - Website Works

I am creating a website that will check if the site works and if it lives. I pass in the URL of the site I would like to check, and the following code checks if the site is alive, and returns an HTTP response code, as well as true or false.

function urlExists($url=NULL)
{
    if($url == NULL) return false;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($httpcode == 0) {
        return array (false, $httpcode);
    }
    else if($httpcode < 400){
        return array (true, $httpcode);
    } else {
        return array (false, $httpcode);
    }
}

With one of the sites I'm testing, I get an HTTP response code of 0, although I know that the site is up and running.

The site is very slow, because its large site is on a not very powerful server, so the response time can vary from 7 to 25 seconds.

Any help would be greatly appreciated.

Thank,

Sam

+4
source share
3 answers

Based on these two links: -

https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html

AND

https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html

: - set maximum time the request is allowed to take

: - timeout for the connect phase

, Site URL , 7-25 . CURL request - .

, .

.

+4

2 - curl() 3 , , / .

A ( php), fopen():

if (!$fp = fopen($url, 'r')) 
{
    trigger_error("Unable to open URL ($url)", E_USER_ERROR);

}
$headers = stream_get_meta_data($fp);
fclose($fp);    
$http_header_info = $headers['wrapper_data'][0];
$httpCode = (int)substr($http_header_info, 9, 3);   


B (php5 +):

$headers = get_headers($url, 1);
$http_header_info = $headers[0];
$httpCode = substr($http_header_info, 9, 3);

, - 3 , , ( HTTP-, )

+2

Code 0 is often returned when using invalid URL syntax or a host error not found.

You can also call curl_error ($ ch) ( http://php.net/manual/en/function.curl-error.php ) to determine the details of the error.

0
source

All Articles