利用php监控网页变动email邮件提醒功能

2025-07-30 0 29

今天给大家出个监控网页变动,如有变动email提醒,如果没有变动就不提醒。

准备工具

1、确保服务器安装php(建议7.4+版本)

2、安装PHPMailer(用于发送邮件)

安装PHPMailer参考命令

cd /www/wwwroot/your_site/monitor/
wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
unzip master.zip
mv PHPMailer-master PHPMailer
rm master.zip

把文件解压到:/www/server/php/74/lib/php/这个文件夹里面

然后将下面php代码新建一个文件,例如:将脚本保存为web_monitor.py

<?php
// 网页变动监控脚本
// 保存路径: /www/wwwroot/your_site/webpage_monitor.php

require ‘/www/server/php/74/lib/php/PHPMailer/src/Exception.php’;
require ‘/www/server/php/74/lib/php/PHPMailer/src/PHPMailer.php’;
require ‘/www/server/php/74/lib/php/PHPMailer/src/SMTP.php’;

// 配置部分
$config = [
‘url_to_monitor’ => ‘https://example.com’, // 要监控的网页URL
‘hash_file’ => ‘/www/wwwroot/your_site/webpage_hash.txt’, // 存储哈希值的文件
’email_settings’ => [
‘host’ => ‘smtp.qq.com’, // SMTP服务器
‘port’ => 465, // SMTP端口(SSL一般为465)
‘secure’ => ‘ssl’, // 加密方式
‘username’ => ‘mianfeicms@qq.com’, // 发件邮箱
‘password’ => ‘password’, // SMTP密码/授权码
‘from’ => ‘mianfeicms@qq.com’, // 发件人邮箱
‘from_name’ => ‘网页监控系统’, // 发件人名称
‘to’ => ‘mianfeicms@qq.com’ // 收件邮箱
],
‘test_mode’ => false // 设为true测试邮件发送,不会实际监控网页
];

// 获取网页内容
function fetchWebpage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$content = curl_exec($ch);

if (curl_errno($ch)) {
throw new Exception(‘CURL错误: ‘ . curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
throw new Exception(“HTTP错误代码: $httpCode”);
}

curl_close($ch);
return $content;
}

// 发送邮件通知
function sendEmailNotification($config, $subject, $body) {
$mail = new PHPMailer\PHPMailer\PHPMailer(true);

try {
// 服务器设置
$mail->SMTPDebug = 0; // 调试模式关闭
$mail->isSMTP();
$mail->Host = $config[’email_settings’][‘host’];
$mail->Port = $config[’email_settings’][‘port’];
$mail->SMTPSecure = $config[’email_settings’][‘secure’];
$mail->SMTPAuth = true;
$mail->Username = $config[’email_settings’][‘username’];
$mail->Password = $config[’email_settings’][‘password’];

// 收件人
$mail->setFrom($config[’email_settings’][‘from’], $config[’email_settings’][‘from_name’]);
$mail->addAddress($config[’email_settings’][‘to’]);

// 内容
$mail->Subject = $subject;
$mail->Body = $body;

return $mail->send();
} catch (Exception $e) {
throw new Exception(“邮件发送失败: ” . $mail->ErrorInfo);
}
}

// 主程序
try {
if ($config[‘test_mode’]) {
// 测试模式 – 只发送测试邮件
$result = sendEmailNotification(
$config,
‘网页监控系统测试邮件’,
‘这是一封测试邮件,表示邮件发送功能正常。’
);

if ($result) {
echo “测试邮件发送成功!\n”;
} else {
echo “测试邮件发送失败\n”;
}
exit;
}

// 正常监控模式
echo “开始监控网页: {$config[‘url_to_monitor’]}\n”;

// 获取当前网页内容
$currentContent = fetchWebpage($config[‘url_to_monitor’]);
$currentHash = md5($currentContent);

// 读取之前保存的哈希值
$previousHash = file_exists($config[‘hash_file’]) ? file_get_contents($config[‘hash_file’]) : ”;

if ($currentHash === $previousHash) {
echo “网页内容未发生变化。\n”;
exit;
}

// 网页内容发生变化
echo “检测到网页内容变化!\n”;

// 发送邮件通知
$subject = “网页内容变化通知 – {$config[‘url_to_monitor’]}”;
$body = “监控的网页内容已发生变化:\n\nURL: {$config[‘url_to_monitor’]}\n\n”;
$body .= “变化时间: ” . date(‘Y-m-d H:i:s’) . “\n\n”;
$body .= “你可以访问该网页查看具体变化。”;

$result = sendEmailNotification($config, $subject, $body);

if ($result) {
// 保存新的哈希值
file_put_contents($config[‘hash_file’], $currentHash);
echo “邮件通知已发送,并更新了哈希值。\n”;
} else {
echo “邮件发送失败,哈希值未更新。\n”;
}

} catch (Exception $e) {
echo “错误: ” . $e->getMessage() . “\n”;
}
?>

具体你需要多久检测一次可以使用宝塔面板的Shell脚本

脚本内容如下

/usr/bin/php /www/wwwroot/your_site/webpage_monitor.php >> /www/wwwroot/your_site/webpage_monitor.log 2>&1

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

版权声明:所有的源码、软件和资料,不得使用于非法商业用途,不得违反国家法律,一切关于该资源商业行为与本站无关。

免费cms模板 建站百科 利用php监控网页变动email邮件提醒功能 https://www.mianfeicms.com/470.html

相关文章

发表评论
暂无评论