Home  >  Article  >  Backend Development  >  Planned (timed) tasks implemented in PHP

Planned (timed) tasks implemented in PHP

WBOY
WBOYOriginal
2016-07-25 08:46:081080browse
Sometimes in order to adjust the interface regularly, the program needs to run automatically. From the Internet, there are two ways to achieve this: 1. ignore_user_abort() The ignore_user_abort() function is combined with set_time_limit(0) and sleep($interval) to realize the automatic running and updating of the program.
  1. //Even if the Client is disconnected (such as closing the browser), the PHP script can continue to execute.
  2. ignore_user_abort();
  3. //The execution time is unlimited. The default execution time of PHP is 30 seconds. By set_time_limit(0) allows the program to execute without limit
  4. set_time_limit(0);
  5. // Run every 5 minutes
  6. $interval=60*5;
  7. do{
  8. $url = “http://www.xxx. con";
  9. $ch = curl_init();
  10. curl_setopt($ch, CURLOPT_URL, $url);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12. curl_setopt($ch, CURLOPT_TIMEOUT, 2);
  13. $result = curl_exec ($ch);
  14. curl_close($ch);
  15. // Wait for 5 minutes
  16. sleep($interval);
  17. }while(true);
Copy code

PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn