本教程使用的定时任务基于EasyTak,EasyTask官方文档:https://gitee.com/392223903/EasyTask
(1).安装tp5.0或者5.1
composer create-project topthink/think=5.0.* tp5 --prefer-dist
(2).安装定时任务composer包
composer require easy-task/easy-task
(3).创建命令行处理类文件application/common/command/Task.php
<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
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'));
// 配置任务
$task = new \EasyTask\Task();
$task->setRunTimePath('./runtime/');
$task->addFunc(function () {
$url = 'https://blog.20230611.cn/?id=319';
file_get_contents($url);
}, 'request', 10, 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).将上面创建的Task.php在配置文件application/command.php中配置一下
return [ 'app\common\command\Task', ];
(5).执行命令(windows请使用cmd):
php think task start 启动命令 php think task status 查询命令 php think task stop 关闭命令 php think task stop force 强制关闭命令
上面创建的定时任务是每隔10秒访问2次网站地址。
提示:后台执行失败可修改为前台启动查看问题或者查看日志文件,有问题可以在qq群反馈bug,记得用星星支持我们
逛公众号文章看到文章"php实现事件监听与触发的方法,你用过吗?",我就好奇了,php又不是asp.net的webform,哪里来的服务端事件监听。于是学习了一波。先看下监听类:class Event { /** &nbs...
php arrayaccess 官方的说法是让你能以数组的形式访问对象,对于这种php内置接口一直不太明白有什么用,坚持多看文章,终于理解,特来分享,思路不同,更易于理解。(1).创建一个学生类,并且实现arrayaccess 接口。<?php class studen...
(1).学习的目标:学会创建父子进程,并且能够区分当前进程是父还是子;了解父进程执行过程,子进程执行过程;能够用多进程执行任务(2).相关函数学习: (2.1)pcntl_fork()执行时: &nbs...
elasticsearch的操作都是基于http协议的,已经有现成的php类库,composer安装即可。{ "require": { &...
PHP7中,通过生成器委托(yield from),可以将其他生成器、可迭代的对象、数组委托给外层生成器。外层的生成器会先顺序 yield 委托出来的值,然后继续 yield 本身中定义的值。同时yield from也能获取到生成器的返回值...
在调用微信code换openid的接口curl报错curl Received HTTP code 403 from proxy after CONNECT,错误码56。可以看到是curl的代理有问题。然后我自己电脑设置代理去访问curl请求的地址,的确也返回了403,说明代理不允许访问这个地址,联系...