如何测试远程服务器是否停机进行维护,即使可能会重定向到维护页面


How to test if a remote server is down for maintenance even though there may be a redirect to a maintenance page?

我们有一个依赖于远程服务的应用程序。我被要求实现一些代码,如果远程web服务器停机(由于维护或闪光),我会显示一条适当的消息。

手头的问题是,当远程设备停机进行维护时,它们通常会重定向到另一个页面。那么,我该如何在PHP中实现一个强大的函数,该函数可以判断特定的URL是否已经启动并运行,而不是重定向到一个伪页面。

谢谢

只需检查响应文本。如果响应包含重定向url中存在的任何文本,那么它肯定处于维护模式。

如果远程web服务器关闭,您也可以检查它。看见https://stackoverflow.com/questions/9144825/php-check-if-a-site-is-down/9145124#9145124

只需检查HTTP返回代码。这可能与卷曲有关,例如:

CURLINFO_HTTP_CODEhttp://de2.php.net/manual/en/function.curl-getinfo.php

<?php
$success = 0;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// Check if any error occured
if(!curl_errno($ch))
{
 if(curl_getinfo($c, CURLINFO_HTTP_CODE) === 200)
    $success = 1;
}
// close cURL resource, and free up system resources
curl_close($ch);
?>