场景:模拟验证码发送。仅做代码演示。
(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并向接口提交手机号进行测试。
1、允许单个域名访问指定某域名(http://client.runoob.com)跨域访问,则只需在http://server.runoob.com/server.php文件头部添加如下代码:header('Access-Control-Allow-Origin:http://client....
上家公司开发医院挂号系统,系统采用GBK编码。ajax发送的中文用户名让PHP保存为cookie出现乱码的解决方案。1.Javascript变量var user=document.getElementById('user').innerText; user=escape(u...
php多进程应用场景主要是非web端,fpm下是不支持多进程的,非类linux操作系统都不支持,请在cli模式使用.可以使用多进程做任务分发,批量计算,批量文件处理,批量爬虫,网络运维等等。下面看一份简单的入门demo//创建子进程 $pid=pcntl_fork(); //返回-1,创建失败,不...
面试中PHP面试官会问调用一个不存在的方法,如何知道是哪个文件哪行调用的?假设方法是getWorkLoad()回答1:开启PHP错误输出,PHP会输出Fatal error: Call to undefined function getWorkLoad() in D:\wwwroot\thinkpa...
(1).config.php 配置文件<?php /** * RabbitMQ_Config */ $config = [ 'host' => ...
参数中包含gb2312的字符串,返回结果是false或者null(不同PHP版本具有差异性)代码:<?php $dbms = 'mysql'; $host = '192.168.8.8'; $dbName =&n...