Planned (timed) tasks implemented in PHP

WBOY
Release: 2016-07-25 08:46:08
Original
1067 people have browsed it
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


source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!