即使可能有重定向到维护页面,如何测试远程服务器是否已关闭以进行维护?

时间:2012-03-21 16:05:30

标签: php webserver

我们有一个依赖于远程服务的应用程序。我被要求实现一些代码,如果远程Web服务器关闭(由于维护或闪烁),我会显示相应的消息。

目前的问题是,当远程维护时,它们通常会重定向到另一个页面。那么我如何在PHP中实现一个强大的功能,可以判断特定的URL是否已启动并运行,而不是重定向到虚拟页面。

谢谢

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

只需检查HTTP返回码即可。例如,curl可以实现这一点:

CURLINFO_HTTP_CODE http://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);
?>