Last Updated: May 31, 2021
·
1.394K
· milos_matic

Check for valid (not dead) links using PHP

function is_available($url, $timeout = 30) {
    $ch = curl_init(); // get cURL handle
    $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;
}