1. 程式人生 > >PHP監控網站能否正常訪問

PHP監控網站能否正常訪問

<?php
header('Content-type: text/html; charset=utf-8');

//網站列表
$url=array(
    'http://www.baidu.com',
    'http://www.sina.com',
    'http://www.qq.com/aa.php',
);

foreach ($urlList as $url) {
    $resCode=getCode($url);
    if (strpos($resCode,'200')===false) {

        echo "網站$url不能正常訪問,錯誤碼:$resCode<br/>";
    }else
    {
        echo "網站$url正常訪問!<br/>";
    }
}



function GetCode($url)
{
    $ch = curl_init ();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 200);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_exec($ch);
    $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $httpCode;
}
?>