PHP实现发送短信验证码

    public function Yzm($Tel,$User_id){
        $ma = mt_rand(100000,999999);
        $code = '【多多赢家】您的短信验证码:'.$ma;
        $url = "http://发送短信服务器:端口/Service.asmx/SendMessageStr?Id=3101&Name=备用账号&Psw=512113&Message=".$code."&Phone=".$Tel."&Timestamp=0";
        $url = file_get_contents($url);
        $par = explode(',',$url);
        $res = [];
        foreach ($par as $p){
            list($k,$v) = explode(':',$p);
            $res[$k] = $v;
        }
        if(isset($res['State']) && $res['State'] == "1"){
            session('Tel',$Tel);
            session('Code',$ma);
            M('User')->where(['id'=>$User_id])->setInc('phone_num',1);
            return true;
        }else{
            return false;
        }
    }
 1 <?php
 2 /**
 3  * 手机验证码
 4  * User: 张萌
 5  * Date: 2019年7月17日, 0017
 6  * Time: 上午 09:39:37
 7  */
 8 
 9 namespace app\admin\controller;
10 
11 use think\Controller;
12 
13 class Sms extends Controller
14 {
15     const APPKEY='key';
16     const APPCODE='code';
17 
18     /**
19      * 通过手机号获取验证码
20      * @param null $tel 手机号
21      * @return bool
22      * @creator(伪代码创建者) 张萌 2019-07-17
23      * @writer(实际开发者) 张萌 2019-07-17
24      * @version_updating(版本更新记录) 暂无
25      */
26     public function getCode($tel=null){
27         if(!$tel)
28             return null;
29         $sms_code=$this->rand_str();
30         $host = "https://smsapi.api51.cn";
31         $path = "/code/";
32         $method = "GET";
33         $appcode = self::APPCODE;
34         $headers = array();
35         array_push($headers, "Authorization:APPCODE " . $appcode);
36         $querys = "code=".$sms_code."&mobile=".$tel;
37         $bodys = "";
38         $url = $host . $path . "?" . $querys;
39 
40         $curl = curl_init();
41         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
42         curl_setopt($curl, CURLOPT_URL, $url);
43         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
44         curl_setopt($curl, CURLOPT_FAILONERROR, false);
45         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
46         curl_setopt($curl, CURLOPT_HEADER, true);
47         if (1 == strpos("$".$host, "https://"))
48         {
49             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
50             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
51         }
52         $result=curl_exec($curl);
53         $message_start=stripos($result,'{');
54         $message_end=stripos($result,'}');
55         $message=substr($result,$message_start);
56         $message=json_decode($message);
57         if($message->success)
58             return $sms_code;
59         else
60             return null;
61     }
62 
63 
64     /**
65      * 生成随机字符串
66      * @param int $randLength 长度
67      * @param int $includeletter 是否包含字母
68      * @return string
69      * @creator(伪代码创建者) 张萌 2019-07-17
70      * @writer(实际开发者) 张萌 2019-07-17
71      * @version_updating(版本更新记录) 暂无
72      */
73     public function rand_str($randLength = 6, $includeletter = 0)
74     {
75         if ($includeletter) {
76             $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';
77         } else {
78             $chars = '0123456789';
79         }
80         $len = strlen($chars);
81         $randStr = '';
82         for ($i = 0; $i < $randLength; $i++) {
83             $randStr .= $chars[mt_rand(0, $len - 1)];
84         }
85         $tokenvalue = $randStr;
86         return $tokenvalue;
87     }
88 }

猜你喜欢

转载自www.cnblogs.com/qiusanqi/p/12145200.html