thinkphp5 实现短信注册验证功能

思路:先注册一个短信验证平台(注册地址:https://s1.chanyoo.net/registers.aspx)六块钱一百条,够测试用了,获取api接口,设置参数,然后请求接口,留意的地方,模版得按照官网给你的,否则不能通过,模版有好多个,找合适自己的,支持post和get,我是测试的时候用get,如果账号密码申请到了,可以直接使用我的接口 ,测试通过噢

public function Msm(){
        //判断是否是get请求方式
        if(Request::instance()->isGet()){
            $phone = Request::instance()->get('phone');
            $rand = rand(111111,999999);
            $url = 'https://api.chanyoo.cn/utf8/interface/send_sms.aspx?username=shiye&password=123456&receiver='.$phone.'&content=您的手机号:'.$phone.',验证码:'.$rand.',请及时完成验证,如不是本人操作请忽略。【变量】';
            $fileGet = file_get_contents($url);
            //发送请求成功之后,解析xml
            $xml = simplexml_load_string($fileGet);
            $data = json_decode(json_encode($xml),true);
            dump($data);
            if($data["message"] == "短信提交成功"){
                $content = ['phone' => $phone,'rand' => $rand];
                $inset = Db::name('userinfo')->insert($content);
                if($inset){
                    return json(['status' => '200','msg' => $data["message"]]);
                }
            }else{
                return json(['status' => $data["result"],'msg' => $data["message"]]);
            }

        }
    }

 

你可能感兴趣的:(PHP)