以下是用 PHP 编写的获取网站 URL 网站状态的代码,并根据 HTTP 状态码判断网站运行状态:

<?php
function getWebsiteStatus($url) {
  $handle = curl_init($url);
  curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  curl_exec($handle);
  $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  curl_close($handle);

  if ($httpCode >= 200 && $httpCode < 400) {
    return 'Website is up and running.';
  } elseif ($httpCode >= 400 && $httpCode < 500) {
    return 'Website is experiencing client error.';
  } elseif ($httpCode >= 500 && $httpCode < 600) {
    return 'Website is experiencing server error.';
  } else {
    return 'Unable to determine website status.';
  }
}

$url = 'https://example.com';
$status = getWebsiteStatus($url);
echo $status;
?>

该代码使用 cURL 库来执行 HTTP 请求并获取网站的 HTTP 状态码。根据 HTTP 状态码的范围,返回相应的网站状态信息。

请确保在运行代码之前已经安装并启用了 cURL 库。

PHP 获取网站状态代码 - 检查网站是否正常运行

原文地址: https://www.cveoy.top/t/topic/bx1c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录