如何使用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在后台运行进程?答案 0 :(得分:2)
1)用PHP替换你的PHP中的最后一个
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
});
更新:
webpage.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
<?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);
?>
答案 1 :(得分: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>
但是你可能不希望在onLoad上调用AJAX,因为你正在寻找更高的效率。
我认为这会有所帮助(如果我理解你的问题)。