PHP 如果,每 30 分钟一次?
PHP if, just once every 30 min?
我有一个带有 IF 语句的脚本,它检查 XML 的特定值。该文档每 30 秒刷新一次,如果在 XML 中找到该值,则执行该命令。有没有办法让脚本每 15 分钟执行一次,而不是每次 XML 每 30 秒刷新一次?
$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.
}
对于此类周期性元素,您必须使用 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
之类的东西
$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
}
}
类似的东西。
我有一个带有 IF 语句的脚本,它检查 XML 的特定值。该文档每 30 秒刷新一次,如果在 XML 中找到该值,则执行该命令。有没有办法让脚本每 15 分钟执行一次,而不是每次 XML 每 30 秒刷新一次?
$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.
}
对于此类周期性元素,您必须使用 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
$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
}
}
类似的东西。