场景:模拟验证码发送。仅做代码演示。
(1).创建一个验证码发送接口sendCaptcha
/** * 发送验证码 */ public function sendCaptcha() { //外部参数(获取手机号) $mobile = $_REQUEST['mobile'] ?? 0; if (!$mobile) { exit(json_encode(['code' => -1, 'msg' => '手机号码不得为空'], 256)); } //生成短信验证码(随机数4位) $captcha = rand(1111, 9999); //组装队列数据Json $send_data = [ 'mobile' => $mobile, 'captcha' => $captcha, ]; //连接本地的Redis 服务 $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); //向Redis的send_captcha队列投递数据 $isPush = $redis->lPush('send_captcha', json_encode($send_data)); if (!$isPush) { exit(json_encode(['code' => -1, 'msg' => '验证码发送失败'], 256)); } //输出发送成功 exit(json_encode(['code' => 0, 'msg' => '验证码发送成功'], 256)); }
(2).EasyTask中的代码如下
// 添加执行定时器 $time = 1; $task->addFunc(function () { //连接本地的Redis 服务 $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); //提取队列中的数据 $data = $redis->rPop('send_captcha'); if ($data) { //提取数据中的手机号和验证码 $data = json_decode($data, true); $mobile = $data['mobile']; $captcha = $data['captcha']; //进行发送,此处为伪代码 //sendCode($mobile,$captcha); //输出日志 echo "向{$mobile}发送验证码{$captcha}成功" . PHP_EOL; } }, 'send_captcha_timer', $time, 1);
(3).启动EasyTask并向接口提交手机号进行测试。
<?php //对比$this和self /* * $this更倾向于对象本身 * */ class Par{ public  ...
<!doctype html> <html> <head> <meta charset="utf-8"> <title>demo</title> </head> <bod...
应用场景:PHP模拟购买,商品数量大于0才能购买常见代码:<?php //连接数据库 $con=mysqli_connect("localhost","ihuohuo","927464cy","ihuohuo");...
if($_SERVER['REQUEST_METHOD'] == 'POST') { echo('This is post '); } elseif ($_SERVER['...
ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...
今天帮朋友查询wordpress执行超级慢的原因,特此记录开启fpm的慢日志,记录执行超过30秒的脚本request_slowlog_timeout = 30 slowlog = var/log/slow.log查看日志[23-May-2019 17...