本教程使用的定时任务基于EasyTak,EasyTask官方文档:https://gitee.com/392223903/EasyTask
(1).安装tp6
composer create-project topthink/think tp
(2).安装定时任务composer包
composer require easy-task/easy-task
(3).创建命令行处理类文件
php think make:command Task task
会生成文件:tp\app\command\Task.php
将Task.php文件内容修改如下:
<?php declare (strict_types=1); namespace app\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; class Task extends Command { protected function configure() { //设置名称为task $this->setName('task') //增加一个命令参数 ->addArgument('action', Argument::OPTIONAL, "action", '') ->addArgument('force', Argument::OPTIONAL, "force", ''); } protected function execute(Input $input, Output $output) { //获取输入参数 $action = trim($input->getArgument('action')); $force = trim($input->getArgument('force')); // 配置任务,每隔20秒访问2次网站 $task = new \EasyTask\Task(); $task->setRunTimePath('./runtime/'); $task->addFunc(function () { $url = 'https://blog.20230611.cn/?id=327'; file_get_contents($url); }, 'request', 20, 2);; // 根据命令执行 if ($action == 'start') { $task->start(); } elseif ($action == 'status') { $task->status(); } elseif ($action == 'stop') { $force = ($force == 'force'); //是否强制停止 $task->stop($force); } else { exit('Command is not exist'); } } }
(4).配置tp\config\console.php文件
<?php // +---------------------------------------------------------------------- // | 控制台配置 // +---------------------------------------------------------------------- return [ // 指令定义 'commands' => [ 'task' => 'app\command\Task', ], ];
(5).执行命令(windows请使用cmd):
php think task start 启动命令 php think task status 查询命令 php think task stop 关闭命令 php think task stop force 强制关闭命令
提示:后台执行失败可修改为前台启动查看问题或者查看日志文件,有问题可以在qq群反馈bug,记得用星星支持我们哦
PHP验证码不显示的问题应该是经常的事情,我在这里把2种解决方案都发出来方便大家后期使用.因为是昨天晚上自己写验证码都没有显示,我就直接把之前和李炎恢老师上课写的验证码拿来还是不显示,醉了。首先是第一种方法:1、打开服务器安装目录下的php.ini文件;2、去掉;extension=php_gd.d...
通过经纬度转换为城市名称,并获得城市的编号,通常在全国类型的商城中比较通用。//经纬度转城市名称(返回城市id,城市名称) //$ak开发密钥,$lat纬度,$lng经度,$type返回数据类型 public function getCityName($ak,$lat,$lng)...
001源码:/* * $xml_str是xml字符串 */ function xmltoarray($xml_str) { //禁止XML实体扩展攻击 libxml_disable_entity_loader(true); //拒绝包含...
php官方已经提供了Iterator(迭代器)接口,通过网上资料的学习,目前看适合超大集合或者数组提取使用。学习一个函数的实现对比内存占用差距.使用迭代器和普通循环实现range()函数。(1).普通循环实现range()函数。function newrange($low, $h...
(1).config.php 配置文件<?php /** * RabbitMQ_Config */ $config = [ 'host' => ...
(1).学习目标: 了解常见信号类型(百度PHP支持的信号类型),(2).相关函数学习: (2.1).pcntl_signal函数用于设置一个信号管理器接收进程信号,参数1:信号类型,参数2:回调函数,用于在接收到参数1类型的信...