批量扫描文件里的链接是否404(包含curl与get_header实现方式)

<?php
//function chkurl($url){
//    $handle  =  curl_init($url);
//    curl_setopt($handle,CURLOPT_RETURNTRANSFER,TRUE);
//    curl_setopt($handle,CURLOPT_CONNECTTIMEOUT,5); //设置超时时间
//    curl_exec($handle);
//    //检查是否404
//    $httpCode = curl_getinfo($handle,CURLINFO_HTTP_CODE);
//    var_dump($httpCode);
//    if($httpCode == 404){
//        return "不存在";
//    }else{
//        return "存在";
//    }
//    curl_close($handle);
//}

//function http_status($url) {
//    $ch = curl_init();
//    curl_setopt($ch, CURLOPT_URL, $url);
//    curl_setopt($ch, CURLOPT_HEADER, 1);
//    curl_setopt($ch, CURLOPT_NOBODY, 1);
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
//    curl_exec($ch);
//    $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
//    curl_close($ch);
//    return ($status == 404) ? '404' : '正常';
//}
//
//$status = http_status('链接');
//var_dump($status);

//测试
//$url = "https://www.baidu.com";
////$notice = chkurl($url);
////echo $notice;
////var_dump(get_headers($url));
//$status = get_headers($url);
//if (strpos($status[0], '404'))
//{
//    echo "404";
//    echo "<br/>";
////    echo "654654";
//}else{
//    echo $status[0].$url.'   正常访问';
//}


$file = fopen("txt文件目录","r");
while(!feof($file))
{
    $url = fgets($file);
    $status = get_headers(trim($url));
    if (strpos($status[0], '404'))
    {
        echo $status[0]."404";
        echo "<br/>";
    }else{
        echo $url.'正常访问';
        echo "<br/>";
    }
}

fclose($file);
?>
posted @ 2023-01-06 13:42  凌敲  阅读(233)  评论(0)    收藏  举报