本教程使用的定时任务基于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,记得用星星支持我们哦
md5/sha1+salt方式是目前各大cms常用的加密方式,虽然salt安全,但是各大md5网站也在研究这个方向,那么我们应该选择password_hash动态hash来助力,一种密码有多种hash结果.看代码模拟登陆.<?php //01.注册 $user ='zhang...
开发com组件可以用c++,vc++,net,我比较熟悉net,演示用dnet(1).创建项目:启动vs2017,新建项目,选择Visual C# ->Windows桌面->类库通用windows(2). 修改Com项目:点击 项目->项目属性,再点击应用程序->程序集信息,...
面试中PHP面试官会问调用一个不存在的方法,如何知道是哪个文件哪行调用的?假设方法是getWorkLoad()回答1:开启PHP错误输出,PHP会输出Fatal error: Call to undefined function getWorkLoad() in D:\wwwroot\thinkpa...
Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者(publisher),订阅者(subscriber)和频道(channel)。 发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个...
自己的composer已经发布到packagist,但是无法使用composer require easy-task/easy-task来安装,只能在配置文件使用如下方式安装:"require": { "easy...
断点下载的原理:http请求头添加Range参数告诉文件服务器端需要的字节范围例如1个文本文件的字节为1000,第一次请求Range: bytes=0-500第二次请求Range: bytes=501-1000通过每次的请求将返回的流追加写入到文件。注意的项目:断点下载服务器端的每次只返回字节传输的...