每隔10分钟调用一次PHP函数1小时

时间:2013-03-04 11:51:24

标签: php

得到这个代码,应该检查网址是否可用,并给我ping:

<?php
function test ($url){
$starttime = microtime(true);
$valid = @fsockopen($url, 80, $errno, $errstr, 30);
$stoptime = microtime(true);
echo (round(($stoptime-$starttime)*1000)).' ms.';

if (!$valid) {
   echo "Status - Failure";
} else {
   echo "Status - Success";
}
}

test('google.com');
?>

我该怎么做才能这个函数被调用每个让我们说10分钟1小时(总共6次)?

4 个答案:

答案 0 :(得分:1)

取决于:你想只做一次吗?

然后你可以在循环中调用函数,并在每次执行循环后使用sleep()

你想每天都这样做吗?或每周/每月一个特定的日子?使用cron。

答案 1 :(得分:1)

使用简单的for循环和sleep命令:

for ($i = 0; $i < 6; $i++) { // Run the code 6 times
    test($url);
    sleep(10 * 60); // Sleep 10 Minutes
}

答案 2 :(得分:1)

如果您在Windows中使用Unix服务器或Windows任务计划程序,我建议您使用cron jobs

像这样你将能够使用编程任务。

在使用cron的情况下,您可以轻松地执行此操作:

*/10 * * * *  php -f your_relative_or_full_path_URL/params > /dev/null 

答案 3 :(得分:0)

每月发送一次电子邮件。我在每日备份邮件中使用它..

<?php 
  ignore_user_abort(true); //if page is closed its make it live
  for($i=0;$i<=30;$i++)
  {
     $to = "someone@exmple.com";
     $subject = "Test mail $i";
     $message = "Hello! This is a simple email message.";
     $from = "from@example.com";
     $headers = "From:" . $from;
     mail($to,$subject,$message,$headers);
     sleep(24*60);//send mail in every 24 hour. 
}

?>
相关问题