php使用curl判斷網(wǎng)頁404(不存在)的方法

字號(hào):


    本文實(shí)例講述了php使用curl判斷網(wǎng)頁404(不存在)的方法。分享給大家供大家參考,具體如下:
    <?php
    /* php使用curl判斷404
     * Created on 2016-6-22
     * Writer www.jb51.net
     */
     function chkurl($url){
        $handle = curl_init($url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);//設(shè)置超時(shí)時(shí)間
        curl_exec($handle);
        //檢查是否404(網(wǎng)頁找不到)
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
        if($httpCode == 404) {
          return false;
        }else{
            return true;
        }
        curl_close($handle);
     }
     $url="http://www.jb51.net/asdasd.html";
     if(chkurl($url)==true){
         echo "存在";
     }else{
         echo "不存在";
     }
    ?>
    希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。