3

如何使用 AJAX 和 PHP 返回远程服务器的状态?我唯一想要的是让它在后台运行,因为如果我不使用 AJAX,它会使我的网页变慢。

我在这里有 PHP curl 片段,它 ping 远程服务器(例如 Google.com)并返回其状态:

<?php

function Visit($url){
       $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
       $ch=curl_init();
       curl_setopt ($ch, CURLOPT_URL,$url );
       curl_setopt($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch,CURLOPT_VERBOSE,false);
       curl_setopt($ch, CURLOPT_TIMEOUT, 3);
       curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
       curl_setopt($ch,CURLOPT_SSLVERSION,3);
       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
       $page=curl_exec($ch);
       //echo curl_error($ch);
       $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       curl_close($ch);
       if($httpcode>=200 && $httpcode<300)
            return true;
       else return false;
}
$load = "http://www.google.com";
if (Visit($load))
       //Website is up
else
       //Website is down
?>

我希望有人可以指导我解决这个问题。

编辑:对不起,不清楚的问题陈述。如何使用我上面提供的 PHP 函数使用 AJAX 在后台运行进程?

4

2 回答 2

2

1)用你的PHP替换你的最后一个if

header("content-type: application/json");
$status = array("status" => Visit($load)?"Website is up":"Website is down");
echo json_encode($status);

2)

$(function() {
  var tId = setInterval(function() {
    $.get("your php.php?url=...",function(data) { // or $.post
      $("#somecontainer").html(new Date()+":"+data);
    },"json");
  },60000); //test every minute
});

更新:

网页.html

现场演示

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var url = "http://www.google.com";
function checkIt() {
  $("#check").append("<hr/>checking "+url+" at "+new Date());
  $.get("check.php?url="+encodeURIComponent(url),function(data) {
    $("#check").append('<br/>Result:'+data.status);
  })
  .error(function(xhr, status, error) {
    $("#check").append("<br/>An AJAX error occured: " + status + "<br/>Error: " + error);
  });
}
$(function() {
  checkIt();    
  var tId = setInterval(checkIt,60000); //test every minute
});
</script>
</head>
<body>
<div id="check"></div>
</body>
</html>

检查.php

<?php
$url = $_GET['url'];

class URIInfo { 
    public $status;
    public $error;

    private $url;

    public function __construct($url) {
        $this->url = $url;
        $this->setData();
    }

    public function setData() {
      $ch = curl_init($this->url);
      curl_setopt($ch, CURLOPT_NOBODY, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_exec($ch);
      $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      $this->error = curl_errno($ch);

      //echo "http status:".$this->status."\nerror:".curl_errno($ch);
      curl_close($ch);
    }

    public function getStatus() {
        return $this->status;
    }
    public function getError() {
        return $this->error;
    }

    // Other functions can be added to retrieve other information.
}

header("content-type: application/json");
$uri_info = new URIInfo($url);
$error = $uri_info->getError();
if ($error!=0) {
  if ($error==6) $error="URL likely invalid:".$url;
  $status = array("status" => "Error occurred: ".$error);
}
else {
  $isUp = $uri_info->getStatus()==200;
  $status = array("status" => "Website is ". $isUp?"up":"down");
}  
echo json_encode($status);
?>
于 2013-08-01T18:21:14.043 回答
0

你从我第一次看到它的时候改变了这个问题,所以我没有复制你所有的代码,但这是一种方法:

首先,完成您的 PHP(正如其他人所指出的):

$load = "http://www.google.com";
if (Visit($load))
    echo "UP";
else
    echo "DOWN";

其次,让你的 AJAX 看起来像这样:

<head>
<script>
    function getdetails() {
        var xmlhttp;
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("msg").innerHTML = " Website is " + xmlhttp.responseText;
            }
        }
        var URL = "URL-OF-THE-PHP-FILE-ABOVE";
        xmlhttp.open("GET", URL, true);
        xmlhttp.send();
    }
    window.onload=function() {
      getdetails(); 
    }
</script>
</head>
<body>
<p id="msg">Website Status</p>
</body>

但是您可能不想让 AJAX 调用 onLoad,因为您正在寻找更高的效率。

我认为这会有所帮助(如果我正确理解您的问题)。

于 2013-08-01T18:37:52.933 回答