通过检查CURLINFO_HTTP_CODE来检测网站是否已启动,即使网站为false,也会返回200

时间:2012-09-28 19:38:48

标签: php curl

我正在运行EasyPHP和PHP5的Windows 7计算机上测试此代码段,当我运行代码时它仍然返回代码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)

3 个答案:

答案 0 :(得分:0)

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

我不知道您的提供商virginmedia,但使用德国telekom,可以选择在其主页上停用此选项。

一个选项是使用像Google这样的开放/免费DNS服务器,可以通过8.8.8.8和8.8.4.4来实现

我希望有所帮助!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

以下脚本可能有助于任何人在此处结束搜索。

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