阿里云市场发送短信

该代码段展示了如何使用阿里云服务发送短信验证码,并将其存储到Redis中以供后续验证。首先,检查手机号格式,然后生成随机验证码,接着通过GET请求调用阿里云API发送短信。如果发送成功,验证码会被存储到Redis中并设置过期时间。
摘要由CSDN通过智能技术生成
  //阿里云发送短信
    public function send()
    {
        $phone=input('phone','');//手机号
        if(empty($phone))
        {
            $this->BackResult('4', '手机号不能为空');
        }elseif(!preg_match($this->phone,$phone))
        {
            $this->BackResult('5', '手机号格式不正确');
        }
        date_default_timezone_set("PRC");
        $code=rand('100000','999999');
       
        //发消息
        $host = "https://zwp.market.alicloudapi.com";
        $path = "/sms/sendv2";
        $method = "GET";
        $appcode = Config('app.AppCode');
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        
        $content=urlencode('【IMSSVRD小帮手】您的动态密码为:'.$code.',您正在登录,若非本人操作,请勿泄露。');
        $querys = "content={$content}&mobile={$phone}";
        
        
        $bodys = "";
        $url = $host . $path . "?" . $querys;
        
        // echo $url;
        // exit;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        
        $res=curl_exec($curl);
        $res=json_decode($res,true);
        
        // echo '<pre>';
        // print_r($res);
        if($res['error_code']==0)
        {
             //验证码加入redis
            $redis = new \Redis();
            $redis->connect('127.0.0.1', 6379);
            $redis->config("SET","notify-keyspace-events", "Ex");
            $seconds =60*60*24;//过期时间60*15
            $redis->setEx($phone,$seconds,$code);
            $session=$redis->get($phone);
            
            $this->BackResult('0', '发送成功',$session);
        }
        
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值