Home > Backend Development > PHP Tutorial > How does PHP use the server to implement scheduled tasks?

How does PHP use the server to implement scheduled tasks?

慕斯
Release: 2023-04-10 08:46:01
forward
3217 people have browsed it

This article will introduce to you how PHP uses the server to implement scheduled tasks? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How does PHP use the server to implement scheduled tasks?

Use the server to implement simple scheduled tasks, Windows scheduled tasks, Linux cron, suitable for certain characteristics every day Time execution

1. Scheduled access to the specified url under windows

Use a scheduled task to execute the auto.php file under windows, and use curl to request the specified interface in the auto.php file The code to implement

auto.php is as follows

function doCurlGetRequest($timeout = 5){
    $url = 'http://127.0.0.1:81/index.php?s=/Admin/Index/dayBonus.html';
    $con = curl_init((string)$url);
    curl_setopt($con, CURLOPT_HEADER, false);
    curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);

    return curl_exec($con);
}
$result = doCurlGetRequest();
var_dump( $result);
die;
Copy after login

The new bat file command is as follows

C:\phpStudy\PHPTutorial\php\php-7.2.1-nts\php.exe -q C:\zx\auto.php
Copy after login

php path:

C:\phpStudy \PHPTutorial\php\php-7.2.1-nts\php.exe

auto.php path: C:\zx\auto.php

window scheduled task

2.Request url under Linux

Use CronTab to execute regularly on Linux

Execute crontab -e

Enter editing mode to add a line

* * * * curl https://www.aaa.com/aaa.php
Copy after login

The first part is the time, and the latter part is the operation content.

30 * * * *

30 is executed when the number of minutes per hour is 30.

The time parameter consists of the following parts

Time, day, month and week

##The first column indicates minutes 1 to 59. Each minute uses Or */1 means, /n means every n minutes, for example */8 means every 8 minutes

The second column means hour 0~23

The third column means date 1~ 31

The 4th column indicates the month 1~12

The 5th column identifies the week 0~6

Recommended learning:

php video tutorial

The above is the detailed content of How does PHP use the server to implement scheduled tasks?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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