dtwkt46424 2014-12-15 22:03
浏览 37

由于SSL,无法检查网站是上升还是下降

So I'm tryign to check if a site is up or down with curl. So here's my logic

  • Strip URL of protocol and anything else, leave just host. example.com
  • Add http protocol to URL, then follow redirect with curl

So here's some code

if(isset($_GET['url'])){
    $domain = $_GET['url'];

    //Clean up domain
    $input = trim($domain, '/');

    // If scheme not included, prepend it
    if (!preg_match('#^http(s)?://#', $input)) {
        $input = 'http://' . $input;
    }

    $urlParts = parse_url($input);

    // remove www
    $domain = preg_replace('/^www\./', '', $urlParts['host']);

    function addhttp($domain) {
        if (!preg_match("~^(?:f|ht)tps?://~i", $domain)) {
            $domain = "http://" . $domain;
        }
        return $domain;
    }
    $domain = addhttp($domain);

    date_default_timezone_set('America/New_York');
    $date = date('m/d/Y h:i:s a', time());

}

And my function to check if the site is up or down

function isDomainAvailible($domain){
    if(!filter_var($domain, FILTER_VALIDATE_URL)){
        return false;
    }

    $curlInit = curl_init($domain);
    curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curlInit,CURLOPT_HEADER,true);
    curl_setopt($curlInit,CURLOPT_NOBODY,true);
    curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curlInit, CURLOPT_FOLLOWLOCATION, true);

    $response = curl_exec($curlInit);

    curl_close($curlInit);

    if($response)return true;

    return false;
}

Now the issue is, when I do a website that has SSL, therefore having a redirect, I always seem to get that the site is up, even though it's down. Any ideas? Is my logic in the right direction?

  • 写回答

2条回答 默认 最新

  • duanhai7274 2014-12-15 22:15
    关注

    You're setting CURLOPT_FOLLOWLOCATION to TRUE.

    That option does what its name implies: it follows redirection headers sent by the site. You'll need to remove this option (or explicitly set it to FALSE) to "validate" the actual URL.

    On a side note: you're stripping the www. part of the domain name regardless of whether there is an actual DNS A record pointing to the domain name with the same IP address. You might be getting an answer from a different machine when doing that.

    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写