«
PHP判断URL链接是否有效可指定超时

时间:2022-11-24    作者:Deri    分类: PHP


指定URL和超时秒数,返回true/false

/**
 * 检测网址连接是否可用,设置超时的时间[单位:秒]
 **/
function check_url($url,$timeout){
    $httpcode = 0;
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_exec($ch);
    # 获取状态码赋值
    $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($httpcode == 200){
        return true;
    }else{
        return false;
    }
}

PHP