38769-vm/api/ping.php
2026-02-25 23:13:00 +00:00

53 lines
1.3 KiB
PHP

<?php
// Proxy script to bypass CORS and measure latency
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$url = $_GET['url'] ?? '';
if (empty($url)) {
echo json_encode(['error' => 'URL is required', 'status' => 0, 'time' => 0]);
exit;
}
// Validate URL format
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo json_encode(['error' => 'Invalid URL format', 'status' => 0, 'time' => 0]);
exit;
}
$start = microtime(true);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'NativeUptimeMonitor/2.0');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
curl_close($ch);
$end = microtime(true);
$timeMs = round(($end - $start) * 1000);
// Determine final status code
$statusCode = $info['http_code'];
if ($error) {
echo json_encode([
'url' => $url,
'status' => 0, // 0 for network/timeout error
'time' => $timeMs,
'error_detail' => $error
]);
} else {
echo json_encode([
'url' => $url,
'status' => $statusCode,
'time' => $timeMs
]);
}
?>