yii中使用云片短信验证---demo

yii中使用云片短信验证---demo

1、打开目录,将云片插件放入vendor文件夹中

2、在web/index.php入口文件中引入插件就可以在控制器使用咯


3、后台:

namespace app\modules\index\controllers;
use app\modules\index\models\MsgModel;
use yii;
use yii\base\Controller;


class MsgController extends Controller
{

    public function actionInfo(){
        //手机号码----前台传过来的
        $requst=yii::$app->request->post();
        $tel=$requst["tel"];
        //验证码
        $code=ceil(rand(123456,987610));
        $code=123456;
        //存入cookie,方便前台对比,前台通过ajax获取
        $cookie = new yii\web\Cookie();
        $cookie->name = 'code';                //cookie名
        $cookie->value = $code;              //cookie值
        $cookie->expire = time() + 3600;       //过期时间
        Yii::$app->response->getCookies()->add($cookie);
        $cookie1 = yii::$app->response->cookies;

        //获取用户信息
        $userOperator = new \UserOperator();
        $result = $userOperator->get();
        // 模板
       $tplOperator = new \TplOperator();
        $result = $tplOperator->get_default(array("tpl_id"=>'2133028'));
        $result = $tplOperator->get();
        $result = $tplOperator->add(array("tpl_content"=>"【急急急】您的验证码是#code#"));
        // 发送单条短信
        $smsOperator = new \SmsOperator();
        $data['mobile'] = $tel;
        $data['text'] = '【急急急】您的验证码是'.$code;
        $data["cookie"]=$cookie1->getValue('code');
        $result = $smsOperator->single_send($data);return $result}}
4、前台使用ajax接收或其他方式均可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值