繁体   English   中英

检查网站是否存在,google.com返回false

[英]Checking if a website exists, returning false for google.com

我正在使用以下方法检查网站是否存在:

function urlExists($url = NULL) {  
    if ($url == NULL) return false;  
    $ch = curl_init($url);  
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $data = curl_exec($ch);  
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    curl_close($ch);   
    if ($httpcode >= 200 && $httpcode < 300){  
        return true;  
    } else {  
        return false;  
    }  
}  

它似乎对google.com返回false,但对于www.google.com不然。 为什么?

Google有一个页面重定向。 所以也要使用这个选项。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

如果您的链接包含https://使用:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM