接口概述
type=5 和 type=6 接口用于批量查询编号对应的完整网站数据,包括域名地址、网站名称、检测记录等信息。
📝 注意:
- type=5 使用跳转页数据源
- type=6 使用落地页数据源
- 两个接口的参数和返回格式完全相同
- 支持批量查询多个编号
接口端点
GET
type=5
https://zhongkong.icu/python-daohang/api.php?type=5&json=[{"bianhao":"编号1"},{"bianhao":"编号2"}]
GET
type=6
https://zhongkong.icu/python-daohang/api.php?type=6&json=[{"bianhao":"编号1"},{"bianhao":"编号2"}]
返回字段说明
website_name
网站名称
bianhao
网站唯一编号
domain
网站域名地址
timeout_count
超时节点数量(地区屏蔽数)
detection_time
检测时间
added_to_all_time
添加到总记录的时间
通知配置指南
当网站的超时节点数(timeout_count)超过设定阈值时,自动发送Telegram和邮件通知。
完整监控脚本
1创建监控脚本文件
在您的网站根目录创建
monitor.php 文件
2完整PHP监控代码
PHP
/** * 网站监控脚本 - monitor.php * * 环境要求: * - PHP 7.4+ * - 开启的PHP模块:curl, openssl, json * - 如果使用邮件功能,确保mail()函数可用或配置SMTP * - 如果使用高级邮件功能,可安装PHPMailer库 * * 支持灵活配置:仅TG、仅邮件、或两者都使用 * 当timeout_count大于设定阈值时发送通知 */ // ==================== 配置区域 ==================== $config = [ // Telegram配置 (设为false禁用) 'telegram' => [ 'enabled' => true, // 设为true启用Telegram通知 'bot_token' => 'YOUR_BOT_TOKEN', 'chat_id' => 'YOUR_CHAT_ID' ], // 邮件配置 (设为false禁用) 'email' => [ 'enabled' => false, // 设为true启用邮件通知 'smtp_host' => 'smtp.gmail.com', 'smtp_port' => 587, 'username' => 'your-email@gmail.com', 'password' => 'your-app-password', 'from_email' => 'your-email@gmail.com', 'from_name' => '网站监控系统', 'to_email' => 'admin@yourcompany.com' ], // 监控配置 'monitor' => [ 'api_url' => 'https://zhongkong.icu/python-daohang/api.php?type=6&json=[{"bianhao":"g8wOG"},{"bianhao":"CXfKD"}]', 'threshold' => 10, // 超时阈值,大于此值发送通知 'check_latest_only' => true, // 只检查最新记录 'timeout' => 15 // API请求超时时间(秒) ] ]; // ==================== 配置验证 ==================== function validateConfig($config) { if ($config['telegram']['enabled']) { if (empty($config['telegram']['bot_token']) || $config['telegram']['bot_token'] === 'YOUR_BOT_TOKEN') { throw new Exception('Telegram Bot Token 未配置'); } if (empty($config['telegram']['chat_id']) || $config['telegram']['chat_id'] === 'YOUR_CHAT_ID') { throw new Exception('Telegram Chat ID 未配置'); } } if ($config['email']['enabled']) { if (empty($config['email']['to_email'])) { throw new Exception('收件人邮箱未配置'); } if (empty($config['email']['from_email'])) { throw new Exception('发件人邮箱未配置'); } } if (empty($config['monitor']['api_url'])) { throw new Exception('API URL 未配置'); } // 检查日志文件权限 if (!is_writable('monitor.log') && file_exists('monitor.log')) { throw new Exception('监控日志文件不可写'); } if (!is_writable('monitor_error.log') && file_exists('monitor_error.log')) { throw new Exception('错误日志文件不可写'); } } // ==================== Telegram通知类 ==================== class TelegramNotifier { private $botToken; private $chatId; public function __construct($botToken, $chatId) { $this->botToken = $botToken; $this->chatId = $chatId; } public function sendAlert($websiteData) { // 验证数据完整性 if (!$this->validateWebsiteData($websiteData)) { throw new Exception('网站数据不完整,无法发送Telegram通知'); } $message = "🚨 网站监控警报 🚨\n\n"; $message .= "📝 网站名称: " . $websiteData['website_name'] . "\n"; $message .= "🔗 网站域名: " . $websiteData['domain'] . "\n"; $message .= "🆔 网站编号: " . $websiteData['bianhao'] . "\n"; $message .= "⏰ 检测时间: " . $websiteData['detection_time'] . "\n"; $message .= "❌ 屏蔽地区数: " . $websiteData['timeout_count'] . " 个\n"; $message .= "📊 状态: 地区屏蔽数超过阈值\n\n"; $message .= "请及时检查网站状态!"; $apiUrl = "https://api.telegram.org/bot{$this->botToken}/sendMessage"; $postData = [ 'chat_id' => $this->chatId, 'text' => $message, // 移除 parse_mode 或改为 'Markdown' 如果使用Markdown格式 ]; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $apiUrl, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => 'WebsiteMonitor/1.0' ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { throw new Exception("Telegram请求失败: " . $error); } return $httpCode === 200; } private function validateWebsiteData($data) { $requiredFields = ['website_name', 'domain', 'bianhao', 'timeout_count', 'detection_time']; foreach ($requiredFields as $field) { if (!isset($data[$field]) || empty($data[$field])) { return false; } } return true; } } // ==================== 邮件通知类 ==================== class EmailNotifier { private $config; public function __construct($emailConfig) { $this->config = $emailConfig; } public function sendAlert($websiteData) { // 验证数据完整性 if (!$this->validateWebsiteData($websiteData)) { throw new Exception('网站数据不完整,无法发送邮件通知'); } $subject = "🚨 网站监控警报 - " . $websiteData['website_name']; $message = "<html> <head> <style> body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; } .alert { background: #fff3cd; border: 1px solid #ffeaa7; padding: 20px; border-radius: 8px; margin: 20px 0; } .info { margin: 12px 0; padding: 8px 0; border-bottom: 1px solid #f0f0f0; } .critical { color: #dc3545; font-weight: bold; font-size: 1.1em; } .header { color: #856404; font-size: 1.3em; margin-bottom: 15px; } </style> </head> <body> <div class='alert'> <div class='header'>🚨 网站监控警报</div> <div class='info'><strong>网站名称:</strong> " . htmlspecialchars($websiteData['website_name']) . "</div> <div class='info'><strong>网站域名:</strong> <a href='" . htmlspecialchars($websiteData['domain']) . "'>" . htmlspecialchars($websiteData['domain']) . "</a></div> <div class='info'><strong>网站编号:</strong> " . htmlspecialchars($websiteData['bianhao']) . "</div> <div class='info'><strong>检测时间:</strong> " . htmlspecialchars($websiteData['detection_time']) . "</div> <div class='info critical'><strong>屏蔽地区数:</strong> " . htmlspecialchars($websiteData['timeout_count']) . " 个</div> <div class='info'><strong>状态:</strong> 地区屏蔽数超过阈值,请及时检查!</div> </div> </body> </html>"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= "From: " . htmlspecialchars($this->config['from_name']) . " <" . $this->config['from_email'] . ">" . "\r\n"; $headers .= "X-Priority: 1 (Highest)\r\n"; $headers .= "X-Mailer: PHP/" . phpversion(); return mail( $this->config['to_email'], $subject, $message, $headers ); } private function validateWebsiteData($data) { $requiredFields = ['website_name', 'domain', 'bianhao', 'timeout_count', 'detection_time']; foreach ($requiredFields as $field) { if (!isset($data[$field]) || empty($data[$field])) { return false; } } return true; } } // ==================== 监控管理器 ==================== class MonitorManager { private $telegramNotifier; private $emailNotifier; private $config; public function __construct($config) { $this->config = $config; // 初始化Telegram通知器(如果启用) if ($config['telegram']['enabled']) { $this->telegramNotifier = new TelegramNotifier( $config['telegram']['bot_token'], $config['telegram']['chat_id'] ); } // 初始化邮件通知器(如果启用) if ($config['email']['enabled']) { $this->emailNotifier = new EmailNotifier($config['email']); } } public function checkAndNotify($apiData) { $results = [ 'total_checked' => 0, 'alerts_found' => 0, 'telegram_sent' => 0, 'email_sent' => 0, 'errors' => [] ]; // 检查API响应结构 if (!isset($apiData['success'])) { $results['errors'][] = 'API响应格式错误: 缺少success字段'; return $results; } if (!$apiData['success']) { $results['errors'][] = 'API请求失败: ' . ($apiData['message'] ?? '未知错误'); return $results; } if (!isset($apiData['matched_records']) || !is_array($apiData['matched_records'])) { $results['errors'][] = 'API响应格式错误: 缺少matched_records字段或不是数组'; return $results; } // 验证API返回的数据结构 if (empty($apiData['matched_records'])) { $results['errors'][] = 'API返回的记录为空'; return $results; } // 获取每个网站的最新记录 $latestRecords = $this->getLatestRecords($apiData['matched_records']); $results['total_checked'] = count($latestRecords); foreach ($latestRecords as $record) { if (!isset($record['timeout_count'])) { $results['errors'][] = '记录缺少timeout_count字段: ' . ($record['bianhao'] ?? '未知编号'); continue; } if ($record['timeout_count'] > $this->config['monitor']['threshold']) { $results['alerts_found']++; // 发送Telegram通知 if ($this->telegramNotifier) { try { if ($this->telegramNotifier->sendAlert($record)) { $results['telegram_sent']++; } else { $results['errors'][] = 'Telegram发送失败: ' . ($record['website_name'] ?? '未知网站'); } } catch (Exception $e) { $results['errors'][] = 'Telegram异常: ' . $e->getMessage(); } } // 发送邮件通知 if ($this->emailNotifier) { try { if ($this->emailNotifier->sendAlert($record)) { $results['email_sent']++; } else { $results['errors'][] = '邮件发送失败: ' . ($record['website_name'] ?? '未知网站'); } } catch (Exception $e) { $results['errors'][] = '邮件异常: ' . $e->getMessage(); } } } } return $results; } /** * 获取每个网站的最新记录 */ private function getLatestRecords($records) { $latest = []; foreach ($records as $record) { if (!isset($record['bianhao']) || !isset($record['detection_time'])) { continue; // 跳过不完整的记录 } $bianhao = $record['bianhao']; $currentTime = strtotime($record['detection_time']); if (!isset($latest[$bianhao]) || $currentTime > strtotime($latest[$bianhao]['detection_time'])) { $latest[$bianhao] = $record; } } return array_values($latest); } } // ==================== 主执行逻辑 ==================== try { // 0. 验证配置 validateConfig($config); // 1. 初始化监控管理器 $monitor = new MonitorManager($config); // 2. 调用API获取数据(使用更健壮的方式) $context = stream_context_create([ 'http' => [ 'timeout' => $config['monitor']['timeout'], 'user_agent' => 'WebsiteMonitor/1.0' ], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]); $response = @file_get_contents($config['monitor']['api_url'], false, $context); if ($response === false) { throw new Exception('API请求失败: 无法获取数据'); } $apiData = json_decode($response, true); if ($apiData === null) { throw new Exception('API响应JSON解析失败: ' . json_last_error_msg()); } // 3. 执行检查并发送通知 $results = $monitor->checkAndNotify($apiData); // 4. 记录执行日志 $logMessage = date('Y-m-d H:i:s') . " - "; $logMessage .= "检查了 " . $results['total_checked'] . " 个网站,发现 " . $results['alerts_found'] . " 个警报"; if ($config['telegram']['enabled']) { $logMessage .= ",Telegram发送: " . $results['telegram_sent']; } if ($config['email']['enabled']) { $logMessage .= ",邮件发送: " . $results['email_sent']; } if (!empty($results['errors'])) { $logMessage .= ",错误: " . implode('; ', $results['errors']); } $logMessage .= "\n"; file_put_contents('monitor.log', $logMessage, FILE_APPEND | LOCK_EX); // 输出结果(用于调试) if (php_sapi_name() === 'cli') { echo $logMessage; } } catch (Exception $e) { $errorMessage = date('Y-m-d H:i:s') . " - 错误: " . $e->getMessage() . "\n"; file_put_contents('monitor_error.log', $errorMessage, FILE_APPEND | LOCK_EX); if (php_sapi_name() === 'cli') { echo $errorMessage; } }
3手动测试脚本
在浏览器中访问:
查看是否正常执行并发送通知
https://你的域名.com/monitor.php
查看是否正常执行并发送通知
宝塔面板定时任务配置
1登录宝塔面板
访问您的宝塔面板地址,输入用户名密码登录
2找到定时任务
在左侧菜单中找到 定时任务 并点击进入
3添加Shell脚本
点击"添加定时任务",选择Shell脚本类型
Shell
# 每30分钟执行一次监控脚本 curl -s "https://你的域名.com/monitor.php" > /dev/null 2>&1
4设置执行周期
推荐设置:
*/30 * * * *- 每30分钟执行一次0 */1 * * *- 每小时执行一次0 */6 * * *- 每6小时执行一次
5保存并测试
点击"添加"保存任务,然后可以手动执行一次测试
✅ 配置完成检查清单:
- ✅ monitor.php 文件已上传到网站根目录
- ✅ 配置信息已修改为实际值
- ✅ 手动访问测试通过
- ✅ 宝塔定时任务已添加
- ✅ 收到测试通知消息
⚠️ 重要提醒:
- 将配置文件中的示例值替换为您的实际配置
- Gmail需要使用应用专用密码,非账户密码
- 定期检查监控日志,确保脚本正常运行
- 建议设置合理的执行频率,避免频繁请求