Have a Snippet?

Keep, track and share your code snippets with your friends

PHP: Check availability of a website Share on Vkontakte

Check availability of a website

function checkUrl($url) {
    $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $page = curl_exec($ch);
    $resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if (($resp >= 200) && ($resp < 310))
        return true;
    else
        return false;
}

if (checkUrl("http://microsoft.com"))
    echo "Website UP
"; else echo "Website DOWN
"; if (checkUrl("http://microsoft1234567.com")) echo "Website UP
"; else echo "Website DOWN
";


Tag: PHP, Check, availability

1 Comments

  1. Avatar
    pritaeas

    17th May 2013, 04:21

    You can do RETURNTRANSFER 0 because you are not doing anything with it. Also 401 and 403 indicate the website is responding, you just need authorization.