本文基于Laravel Framework 6.17.1版本
(1).创建项目(依赖真多,如此臃肿)
composer create-project --prefer-dist laravel/laravel blog
(2).安装定时任务composer包
composer require think-task/think-task
(3).执行创建命令行处理类文件(自动生成了文件app/Console/Commands/Task.php)
php artisan make:command Task
(4).修改第3步创建的app/Console/Commands/Task.php为以下内容
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Task extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'task{action=start}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$action = $this->argument('action');
// 初始化EasyTask
$task = new \ThinkTask\Task();
// 设置项目名称
$task->setPrefix('Think');
// 设置后台运行
$task->setDaemon(false);
// 设置日志保存目录
$task->setRunTimePath('./bootstrap/cache/');
// 添加闭包任务,开2个进程5s执行1次访问网址
$task->addFunc(function () {
@file_get_contents('http://xingxinghan.cn/?id=6');
}, 'Curl_1', 5, 3);
// 添加执行tp默认控制器的方法,开1个进程10s执行1次访问网址
//$task->addClass('\app\index\controller\Index', 'index', 'Curl_2', 10, 1);
// 根据命令执行
if ($action == 'start')
{
$task->start();
}
if ($action == 'status')
{
$task->status();
}
if ($action == 'stop')
{
$task->stop();
}
}
}(5).将上面的文件注册一下(修改app/Console/Kernel.php文件)
protected $commands = [ \App\Console\Commands\Task::class ];
(6).执行命令(windows请使用cmd,由于cmd不支持utf8编码,可以在本站搜索关键字“乱码”相关文章处理),:
php artisan task start 启动命令 php artisan task status 查询命令 php artisan task stop 关闭命令
提示:后台执行失败可修改为前台启动查看问题或者查看日志文件,有问题可以在qq群反馈bug
如果想在windows中执行php,并且让php脚本在后台运行,可以用下面的cmd命令start /b php D:\wwwroot\default\demo1\run.php例如上面的命令意思后台运行run.php,如果想用php编写异步代码: ...
项目中使用服务来执行webservice,由于对方系统api不稳定,经常导致服务崩溃,只能重启,一个月差不多要重启一次。初期的解决办法是捕获异常,然后continue掉。<?php try { $url = 'http...
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...
第一次听说端口复用是在mixphp最新版本中发现的,mixphp启动监听9501端口,现在作者说可以多开几个进程来执行mixphp,我心里想了下再启动不是会端口冲突嘛,但是却没有问题,于是下载mixphp的源码解读,原来是启动http服务器使用new Co\Http\Server('0.0....
PHP7中,通过生成器委托(yield from),可以将其他生成器、可迭代的对象、数组委托给外层生成器。外层的生成器会先顺序 yield 委托出来的值,然后继续 yield 本身中定义的值。同时yield from也能获取到生成器的返回值...