阿里云短信服务实现短信验证功能

首先申请一个阿里云的帐户,登录后访问如下网址php

https://help.aliyun.com/product/44282.htmlhtml

因为阿里云没有给测试的条数,你们能够买一个9.9的400条的新人套餐服务小程序

接下来访问国内消息选项建立签名和模板,此处须要单位的营业执照和受权书,若是没有的能够试着尝试腾讯云,用本身的小程序注册,也可实现短信验证码,有100条免费测试条数https://blog.csdn.net/qq_29099209/article/details/87632159api

而后访问https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.611.1c4c38d7MYx0MD查看JAVA,PHP,Python,JavaScript,C#如何调用的,我使用的是PHP,这上面也有参数格式,能够参考个人代码dom

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;


class AlibabaController extends Controller
{

    public function AlibabaSms($number)
    {
        Log::info(333333333333);
        Log::info($number);
        AlibabaCloud::accessKeyClient('LTAxxxxxxxxxx', 'j3hwxxxxxxxxxxxxx')
            ->regionId('cn-zhengzhou')// replace regionId as you need
            ->asGlobalClient();
        $TemplateCode = $this->getRandomString(4);
        try {
            $result = AlibabaCloud::rpcRequest()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'RegionId' => 'cn-hangzhou',
                        'PhoneNumbers' => 'xxxxx',发送的电话号,多个以逗号分隔
                        'SignName' => 'xxxxx',签名的名称
                        'TemplateCode' => 'xxxx',模板代码
                        'TemplateParam' => '{"code":"'.$TemplateCode.'"}',发送的短信参数
                    ],
                ])
                ->request();
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

//生成校验码
    public function getRandomString($len, $chars = null)
    {
        if (is_null($chars)) {
            $chars = "0123456789";
        }
        mt_srand(10000000 * (double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

}