" 以下是一个简单的多节点 ping 接口 PHP 代码示例。该代码将允许用户输入要 ping 的 IP 地址或域名,并显示来自所有指定节点的响应时间。
```php
<?php
function ping_multiple_hosts($hosts) {
$responses = [];
foreach ($hosts as $host) {
$start_time = microtime(true);
$response = @ping($host);
$end_time = microtime(true);
if ($response) {
$responses[] = ['host' => $host, 'time' => $end_time - $start_time];
} else {
$responses[] = ['host' => $host, 'time' => 'Unreachable'];
}
}
return $responses;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$hosts = $_POST['hosts'];
$responses = ping_multiple_hosts(explode(',', $hosts));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiple Hosts Ping</title>
</head>
<body>
<h1>Multiple Hosts Ping</h1>
<form method="post">
<label for="hosts">Enter IP addresses or domains separated by commas:</label>
<input type="text" name="hosts" id="hosts" required>
<button type="submit">Ping</button>
</form>
<?php if (isset($responses)): ?>
<h2>Results:</h2>
<table>
<thead>
<tr>
<th>Host</th>
<th>Time (ms)</th>
</tr>
</thead>
<tbody>
<?php foreach ($responses as $response): ?>
<tr>
<td><?= $response['host'] ?></td>
<td><?= $response['time'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</body>
</html>
```
此代码首先定义了一个名为 `ping_multiple_hosts` 的函数,该函数接受一个包含 IP 地址或域名的数组,并使用 `ping` 函数对每个节点执行 ping 操作。然后,它将结果显示在一个数组中,其中每个元素都是一个关联数组,包含主机名和响应时间。
在主程序中,我们检查请求方法是否为 `POST`,如果是,则从表单中获取 `hosts` 字段,并调用 `ping_multiple_hosts` 函数。最后,我们显示结果表格。"