使用AJAX和PHP返回远程服务器状态

Using AJAX with PHP to return remote server status

本文关键字:服务器 状态 返回 AJAX PHP 使用      更新时间:2023-09-26

如何使用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在后台运行进程?

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>

check.php

  • 的实时演示http://google.com
  • 的实时演示http://googleeeee.coma

<?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);
?>

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

首先,完成你的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,因为您正在寻求更高的效率。

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