【问题标题】:Testing if a web site is up by checking CURLINFO_HTTP_CODE returns 200 even if web site is false即使网站为假,通过检查 CURLINFO_HTTP_CODE 来测试网站是否已启动也会返回 200
【发布时间】:2012-09-28 19:38:48
【问题描述】:

我正在运行 EasyPHP 和 PHP5 的 Windows 7 机器上测试此代码 sn-p,当我运行代码时它仍然返回代码 200,我使用的是不存在的 url,所以它不应该,应该?这是因为重定向发生在例如http://www.123-reg.co.uk?这就是我的怀疑,但我正在努力寻找如何解决这个问题。

$url = "http://www.bnkbkvbirbcx.co.uk";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, TRUE); 
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
$hr = curl_exec($ch); 
$httpreturncode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch);
echo $httpreturncode;

插入 var_dump($hr, curl_getinfo($ch)) 会产生这个输出;

string 'HTTP/1.1 200 OK

Date: Fri, 28 Sep 2012 20:18:16 GMT

Server: Apache

Cache-control: no-cache, no-store

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Pragma: no-cache

Connection: close

Content-Type: text/html; charset=UTF-8

' (length=224)

array
'url' => string 'http://www.bnkbkvbirbcx.co.uk' (length=29)
'content_type' => string 'text/html; charset=UTF-8' (length=24)
'http_code' => int 200
'header_size' => int 224
'request_size' => int 62
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 0.063
'namelookup_time' => float 0.016
'connect_time' => float 0.031
'pretransfer_time' => float 0.031
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float 0
'starttransfer_time' => float 0.063
'redirect_time' => float 0
'certinfo' => 
array
  empty
'redirect_url' => string '' (length=0)

【问题讨论】:

  • 我相信 cURL 可以选择禁用重定向。
  • 当我调用一个“假”站点时,我个人将其解释为无效页面,在该站点您会收到错误页面,我将收到响应代码 404,并且不会被重定向。到目前为止,我看不出你的问题是什么......
  • 你现在应该可以看到代码了,它只是和很多例子中的一样,所以我不知道为什么会出现这个问题。
  • 我没有看到从 http://www.bnkbkvbirbcx.co.ukhttp://www.123-reg.co.uk 的任何重定向。
  • var_dump($hr, curl_getinfo($ch));$hr = curl_exec($ch); 之后。给我们输出。向您的 ISP 提出的所有问题。我什至找不到www.bnkbkvbirbcx.co.uk 甚至bnkbkvbirbcx.co.uk 的DNS 记录。看起来他们实现了一些重定向系统,该系统会自动为不存在的域提供 200 个答案。

标签: php curl


【解决方案1】:

我想建议为您的计算机/服务器设置另一个 DNS 服务器。您的提供商似乎将不存在的域重定向到他们的主页。

我不知道您的供应商virginmedia,但德国电信有一个选项可以在他们的主页上停用此选项。

一种选择是使用像 Google 一样的开放/免费 DNS 服务器,可通过 8.8.8.8 和 8.8.4.4 访问

希望对你有帮助!

【讨论】:

  • 无论我做什么来解决这个问题,事实仍然是当我的网站上线时这个 URL
  • 抱歉,简而言之,我会坚持这一点 - 谢谢我需要在网络服务器上测试这个,而不是我的 wamp,现在看来我的 ISP 正在妨碍我,谢谢大家.
【解决方案2】:

好吧,我终于弄明白了,我觉得我花了这么长时间才发现自己很愚蠢!我的 isp 确实干预了一个“方便的功能”,如下所示:“如果您输入的地址没有找到站点,这个方便的功能会将不正确的地址转换为网络搜索,因此您将得到一个列表而不是错误消息我们最接近的比赛”。我还发现我可以选择“关闭”我所做的这个方便的功能,现在我得到了我期望的结果。感谢您的所有输入,正如您从我的一些不稳定的帖子中可以看出的那样,我是 stackoverflow 的新手,但发现分享我的问题很好。

【讨论】:

    【解决方案3】:

    下面的脚本可能对在这里结束搜索的人有所帮助。

    fakePhrase 用于检测 ISP“搜索辅助”广告软件 HTTP 响应。

    #!/bin/bash
    
    fakePhrase="verizon"
    siteList=(
      'http://google.com'
      'https://google.com'
      'http://wikipedia.org'
      'https://wikipedia.org'
      'http://cantgettherefromhere'
      'http://searchassist.verizon.com'
    )
    
    exitStatus=0
    
    function isUp {
      http=`curl -sL -w "%{http_code}" "$1" -o temp_isUp`
      fakeResponse=`cat temp_isUp | grep $fakePhrase`
      if [ -n "$fakeResponse" ]; then
        http=$fakePhrase
      fi
      case $http in
      [2]*)
        ;;
      [3]*)
        echo 'Redirect'
        ;;
      [4]*)
        exitStatus=4
        echo "$1 is DENIED with ${http}"
        ;;
      [5]*)
        exitStatus=5
        echo "$1 is ERROR with ${http}"
        ;;
      *)
        exitStatus=6
        echo "$1 is NO RESPONSE with ${http}"
        ;;
      esac
    }
    
    for var in "${siteList[@]}"
    do
      isUp $var
    done
    
    if [ "$exitStatus" -eq "0" ]; then
      echo 'All up'
    fi
    
    rm temp_isUp
    exit $exitStatus
    

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 2012-05-26
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      相关资源
      最近更新 更多