php 消息队列
- 安装 beanstalkd
yum install -y beanstalkd
- 后台启动
nohup beanstalkd &
- 安装php消息队列包
composer require pda/pheanstalk
简单使用
- 创建生产者
public function index()
{
// 创建队列生产者
$pda = Pheanstalk::create('127.0.0.1');
// dump($pda->stats());
// 模拟数据
$data = [
'price' => 1.00,
'name' => '测试商品'
];
// 投入到管道中 等待消费者消费 参数分别是 1 数据 2优先级 3设置延迟时间处理
$id = $pda->useTube('order')->put(json_encode($data),0,10);
dump($id);
}
- 创建消费者消费任务数据
public function xiaofei()
{
$pda = Pheanstalk::create('127.0.0.1');
// 获取管道并消费
$job = $pda->watch('order')->ignore('default')->reserve();
// 获取任务id
$id = $job->getId();
dump($id);
// 获取任务数据
$data = $job->getData();
dump($data);
// 处理完任务后就删除掉
$pda->delete($job);
}
thinkphp 的命令行
tp6队列消息
composer require topthink/think-queue
//配置文件位于config/queue.php
[
'default'=>'sync' //驱动类型,可选择 sync(默认):同步执行,database:数据库驱动,red
is:Redis驱动,topthink:Topthink驱动
]
验证码生成
composer require topthink/think-captcha
//控制器中
public function captcha($id = '')
{
return captcha($id);
}
//路由定义
\think\facade\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@
index");
//验证
$this->validate($data,[
'captcha|验证码'=>'require|captcha'
]);
//手动验证
if(!captcha_check($captcha)){
//验证失败
};