本教程使用的定时任务基于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 //对比$this和self /* * $this更倾向于对象本身 * */ class Par{ public  ...
function objtoarr($obj){ $ret = array(); foreach($obj as $key =>$value){ if(gettype($value) == 'arr...
在一个正式项目中操作人员提交239个产品信息进行保存,但是系统却提示没有提交239个产品,于是开启错误信息,显示如下:Warning: Unknown: Input variables exceeded 1000. To incr...
开发com组件可以用c++,vc++,net,我比较熟悉net,演示用dnet(1).创建项目:启动vs2017,新建项目,选择Visual C# ->Windows桌面->类库通用windows(2). 修改Com项目:点击 项目->项目属性,再点击应用程序->程序集信息,...
(1).在PHP中可以查看的环境变量包括: (1.1).电脑环境变量 (2.1).服务器环境变量(2).getenv()函数获取一个环境变量的值.参数1是环境变量的key,参数2值为true的时候仅从你的电脑环境变量中查找,参数2值为false会从两种变量中全部查询//获取我电脑登录的用户名,输出A...
<?php /** * daemonize让当前脚本为守护进程执行 * @param string $callback 匿名函数 */ function daemonize($callback) {...