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接收或其他方式均可。


猜你喜欢

转载自blog.csdn.net/awake720/article/details/79782037