How to implement simple scheduled task execution in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:04:02
Original
837 people have browsed it

How to implement simple timed execution tasks in php

This article mainly introduces the implementation method of simple timed execution tasks in php, involving operating techniques such as curl and sleep, and has certain reference points. Value, friends in need can refer to it

The example in this article describes the implementation method of simple scheduled execution of tasks in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

ignore_user_abort();

set_time_limit(0);

$interval = 60*5;

do{

$url = "http://www.sina.com.cn/";

$ch = curl_init();//创建一个新的curl会话

curl_setopt($ch,CURLOPT,$url);//设置需要抓取的cURL

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

//设置cURL参数,要求结果保存到字符串还是页面(1或真表示保存而不输出)

curl_setopt($ch,CURLOPT_TIMEOUT,2);//最大延续2秒

$result = curl_exec($ch);//执行

curl_close($ch);//关闭

sleep($interval);//休息5分钟

}while(true);

?>

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16
<🎜>ignore_user_abort();<🎜> <🎜>set_time_limit(0);<🎜> <🎜>$interval = 60*5;<🎜> <🎜>do{<🎜> <🎜>$url = "http://www.sina.com.cn/";<🎜> <🎜>$ch = curl_init();//Create a new curl session<🎜> <🎜>curl_setopt($ch,CURLOPT,$url);//Set the cURL to be crawled<🎜> <🎜>curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);<🎜> <🎜>//Set cURL parameters and require the result to be saved to a string or a page (1 or true means saving without output)<🎜> <🎜>curl_setopt($ch,CURLOPT_TIMEOUT,2);//Maximum duration 2 seconds<🎜> <🎜>$result = curl_exec($ch);//Execute<🎜> <🎜>curl_close($ch);//Close<🎜> <🎜>sleep($interval);//rest for 5 minutes<🎜> <🎜>}while(true);<🎜> <🎜>?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/966926.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/966926.htmlTechArticleHow to implement simple timed execution tasks in php. This article mainly introduces the implementation method of simple timed execution tasks in php, involving Operation skills such as curl and sleep have certain reference value and need...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template