Prashant
Prashant

Reputation: 392

Better way to check a URL is not dead in PHP?

Currently, I'm using this code for detecting whether a link is dead or not:

function is_available($url, $timeout = 30) {
    $ch = curl_init(); 
    $opts = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $url, CURLOPT_NOBODY => true, CURLOPT_TIMEOUT => $timeout);
    curl_setopt_array($ch, $opts); 
    curl_exec($ch); 
    $retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; 
    curl_close($ch);
    return $retval;
}

but this code is taking too much bandwidth. Is it possible to make it in such a way that it take less amount of bandwidth?

Upvotes: 1

Views: 123

Answers (1)

Jirka Hrazdil
Jirka Hrazdil

Reputation: 4021

Use these to perform a HEAD request than only returns header, not response body, thus reducing bandwith:

curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);

Upvotes: 2

Related Questions