PHP,每30分钟一次



我有一个带有IF语句的脚本,它检查XML中的特定值。该文档每30秒刷新一次,如果在XML中找到该值,则执行该命令。有没有办法使脚本每15分钟执行一次,而不是每30秒刷新一次XML ?

$xml = 'http://www.example.com/xmlfile.xml';
if ($xml->value == '1')
{
// Exectute something if value from the XML is 1.
}
else
{
// Value not 1, do nothing.
}
$fileName = "lastrun.txt";
$time = file_get_contents($filename);
$timeBetweenRuns = 15;
if(!$time || time() > $time+60*$timeBetweenRuns){//if no file or time elapsed more then timeBetweenRuns 
   //Your code read file
   if($xml->1){
       //Your code .. Only thing if it may take more then next run, you need to add lock to not allow two process do same thing
       file_put_contents($filename, time())
   }
   else{//Your code
   }
}

差不多。

对于这样的周期性元素,必须使用CronJob。

0 */30 * * * * php /root/path/to/script.php

在脚本中做你的逻辑。

$xmlPath = 'http://www.example.com/xmlfile.xml';
$xml = new SimpleXMLElement($xmlPath);
$value = $xml->value;
if ($value === '1') {
    // Do stuff.
}

如果你没有Cron访问权限,你可以使用www.setcronjob.com之类的东西而不是PHP脚本访问,使用wget http://example.com/script.php

之类的东西

最新更新