问题描述
我想知道是否有一种方法可以在每台Web服务器上的文件发生更改时在我的Web服务器上发送一个php脚本给我发送电子邮件.
I was wondering if there's a way to have a php script on my web server email me whenever a file from another web server changes.
例如,此文件经常更改: http://media1.clubpenguin.com/play/zh/web_service/game_configs/paper_items.json
For instance, there's this file that changes frequently: http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json
我在博客中介绍一个游戏,该文件对于在竞争对手之前发布更新信息非常重要.我经常忘记检查它.
I blog about a game and that file is very important for creating post on updates before my competitors. I often forget to check it though.
有没有一种方法可以让脚本在文件更新时通过电子邮件发送给我,或者检查该文件是否已更新,并通过电子邮件将其发送给我?
Is there a way to have a script email me whenever that file updates, or check that file to see if it has updated, and email me if it has?
推荐答案
您需要将这两个文件放在同一文件夹中.
You need have this two files in same folder.
- old.json
- scriptForCron.php
在 scriptForCron.php 中写:
$url='http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$execute = curl_exec($ch);
$fp=fopen('old.json','w+');
$oldjson=fread($fp,filesize('old.json'));
if($execute!=$oldjson){
mail('[email protected]','Yoohoo', 'File changed');
fputs($fp,$execute);
}
fclose($fp);
然后将 scriptForCron.php 添加到cron作业中.您可以要求托管支持.
And then add scriptForCron.php to cron job.You can ask hosting support for it.
这篇关于PHP-远程文件更改时的电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!