Home  >  Article  >  php教程  >  使用阿里大鱼短信发送验证码

使用阿里大鱼短信发送验证码

PHP中文网
PHP中文网Original
2016-05-22 17:22:301017browse

php代码

    public function index()
    {
        $client  = new Client;
        $request = new SmsNumSend;

        // 短信内容参数
        $smsParams = [
            'code'    => $this->randString(),
            'product' => '测试的'
        ];

        // 设置请求参数,具体参数可以参考官方文档
        $req = $request->setSmsTemplateCode('SMS_6620027')
            ->setRecNum('18600000000')
            ->setSmsParam(json_encode($smsParams))
            ->setSmsFreeSignName('注册验证')
            ->setSmsType('normal')
            ->setExtend('123456');

        print_r($client->execute($req));
    }

    /**
     * 获取随机位数数字
     * @param  integer $len 长度
     * @return string       
     */
    protected static function randString($len = 6)
    {
        $chars = str_repeat('0123456789', $len);
        $chars = str_shuffle($chars);
        $str   = substr($chars, 0, $len);
        return $str;
    }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn