本教程使用的定时任务基于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,记得用星星支持我们
原理:使用curl_init()创建多个请求实例,再使用curl_multi_init()批量执行创建的多个请求实例。文件1:curl.php<?php $threads=500;//并发请求次数 $url='http://blog.cn/index.php?';...
源码:特别适用于微信支付中通知微信支付网关function array2xml($arr, $level = 1) { $s = $level == 1 ? "<xml&g...
md5/sha1+salt方式是目前各大cms常用的加密方式,虽然salt安全,但是各大md5网站也在研究这个方向,那么我们应该选择password_hash动态hash来助力,一种密码有多种hash结果.看代码模拟登陆.<?php //01.注册 $user ='zhang...
上篇文章已经讲解arrayacces的原理,现在来讲解下arrayaccess的实际应用。一个大型的互联网项目中必然会存在各种配置信息,例如多种数据库信息:mysql,tidb,mongodb,redis,某个业务模块单独的配置信息如比例,额度等等,那么该如何治理配置信息?PHP项目中大部分的框架都...
xmlrpc协议是通过http请求xml数据进行通信。webservice中和它相同的是soap。soap调用的确很简单,但是创建wsdl太繁琐,效率低下。xmlrpc很好的解决这个问题。(1).创建xmlrpc服务端(求和函数api)function getSum($method,$ar...
面试中PHP面试官会问调用一个不存在的方法,如何知道是哪个文件哪行调用的?假设方法是getWorkLoad()回答1:开启PHP错误输出,PHP会输出Fatal error: Call to undefined function getWorkLoad() in D:\wwwroot\thinkpa...