php - 使用带有 PHP 的 AJAX 返回远程服务器状态

标签 php javascript jquery ajax

如何使用 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

Live Demo

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

关于php - 使用带有 PHP 的 AJAX 返回远程服务器状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18001560/

相关文章:

循环中的 PHP 条件 - PHP

php - 调用未定义函数 Symfony\Polyfill\Mbstring\iconv_strlen()

javascript - 如何从特定的实时数据库路径查询值?

jquery - 如何删除链接?

javascript - 如果数据库中存在输入,则重定向到 URL

php - 如果未选择任何内容,则 Elasticsearch 发布值全选

javascript - 使用 prevState setState 失败

javascript - 将多个参数传递给 getJSON 会产生问题

javascript - 在div中导入html页面并执行 "body onload"

javascript - 添加事件监听器