卷曲有效页

卷曲有效页

问题描述:

我有一个检查一个网址,以确保(一)有一些类型的服务器响应,而PHP函数的返回404(B)。它不是一个404卷曲有效页

它对我测试过的每个域/ URL都能正常工作,bostonglobe.com除外,它会返回一个有效URL的404。我猜它与付费墙有关,但是我的功能在nytimes.com和其他报纸网站上运行良好。

下面是返回一个404的一个示例网址:

https://www.bostonglobe.com/news/politics/2016/11/17/tom-brady-was-hoping-get-into-politics-might-time/j2X1onOLYc4ff2LpmM5k9I/story.html

我在做什么错?

function check_url($url){ 
    $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_NOBODY, true); 
    curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); 
    $result = curl_exec($curl); 
    if ($result == false) { 
     //There was no response 
     $message = "No information found for that URL"; 
     } else { 
     //What was the response? 
     $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
     if ($statusCode == 404) { 
     $message = "No information found for that URL"; 
     } else{ 
     $message = "Good"; 
     } 
     } 
    return $message; 
    } 

该问题似乎来自于您CURLOPT_NOBODY选项。

我已测试过您的代码,无论是否使用此行,并且当CURLOPT_NOBODY存在时,http代码将返回404;如果不存在,则返回200

PHP manual告诉我们,设置CURLOPT_NOBODY选项将改变您的申请方法HEAD,我的猜测是,在其bostonglobe.com托管服务器不支持这种方法。

+0

呃......愚蠢的错误。谢谢你为了解开这个谜,罗伯托! – Dave

+0

bostonglobe.com不支持http HEAD请求..我也用CURLOPT_NOBODY测试了代码,它在我的本地主机服务器上工作正常..但它看起来像一个防火墙问题 –

我用curl命令检查了这个URL。

curl --head https://www.bostonglobe.com/news/politics/2016/11/17/tom-brady-was-hoping-get-into-politics-might-time/j2X1onOLYc4ff2LpmM5k9I/story.html 

它返回一个错误。(HTTP/1.1 404未找到)

我也使用另一个命令使用wget。结果是一样的。

wget –server-response --spider https://www.bostonglobe.com/news/politics/2016/11/17/tom-brady-was-hoping-get-into-politics-might-time/j2X1onOLYc4ff2LpmM5k9I/story.html 

我还检查了这种情况与网络服务(HTTP请求发生器:http://web-sniffer.net/)。 结果是一样的。

https://www.bostonglobe.com/中的其他URL情况仅适用于HEAD请求。 但我认为后期页面(扩展名.html)不支持头部请求。

服务器管理员或程序员关机头部请求?

为PHP,

if($_SERVER["REQUEST_METHOD"] == "HEAD"){ 
    // response 404 or using header method to redirect 
    exit; 
} 

或服务器软(Apache和更多)限制HTTP请求。例如,

这样做的目的是减少服务器负载。