即使可能重定向到维护页面,如何测试远程服务器是否因维护而停机? [英] How to test if a remote server is down for maintenance even though there may be a redirect to a maintenance page?

查看:40
本文介绍了即使可能重定向到维护页面,如何测试远程服务器是否因维护而停机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have an application that is dependent upon a remote service. I have been asked to implement some code whereby if the remote web server is down (due to maintenance or glithes) that I display an appropriate message.

手头的问题是,当遥控器因维护而停机时,它们通常会重定向到另一个页面.那么我如何在 PHP 中实现一个健壮的函数,该函数可以判断特定 URL 是否已启动并正在运行,而不是将其重定向到一个虚拟页面.

The issue at hand is that when the remote is down for maintenance, they usually redirect to another page. So how do I get about implementing in PHP a robust function that can tell if a particular URL is up and running as opposed to it being redirected to a dummy page.

谢谢

推荐答案

只需检查 HTTP 返回码即可.例如,这可以通过 curl 实现:

Just check the HTTP return code. This is possible with curl for instance:

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

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

这篇关于即使可能重定向到维护页面,如何测试远程服务器是否因维护而停机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆