早睡早起,方能养生
Sleep early rise early, way to keep healthy

Aliyun短信验证码 ThinkPHP5.1

super
2020-10-30 12:52
views 3378

 composer安装

// 切换阿里云composer源
composer config repo.packagist composer https://mirrors.aliyun.com/composer/
// 安装aliyun短信composer包
composer require alibabacloud/client

 示例:

<?php

namespace app\index\controller;

use app\index\model\Aliyun;

class Index
{
    public function test()
    {
        $phone = input('phone');

        $code = rand(100000, 999999);
    
        $result = Aliyun::mark()->sendCode($phone, $code);
        if ($result) {
            cache('PHONE_CODE_' . $phone, $code, 1800);
            echo json_encode(['code' => 200, 'msg' => 'send success!']);exit;
        } else {
            echo json_encode(['code' => 500, 'msg' => 'send error!']);exit;
        }
    }
}

 class:

<?php

namespace app\index\model;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class Aliyun
{

    public static function mark()
    {
        return new static();
    }

    /**
     * 发送短信验证码 - 阿里云
     * @access public
     * @author super
     * @date 2020-10-30
     * @param string $phoneNumber 手机号码
     * @param int $code 验证码
     * @return boolean
     */
    public function sendCode($phoneNumber, $code)
    {
        $accessKeyId = 'LTA********************'; // key
        $accessSecret = '****************************'; // 注意不要有空格
        $signName = 'XXXXX'; // 配置签名
        $templateCode = 'SMS_XXXXXX'; // 配置短信模板编号

        $jsonTemplateParam = json_encode(['code' => $code]); // 变量替换

        AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)
            ->regionId('cn-hangzhou')
            ->asGlobalClient();
        try {
            $result = AlibabaCloud::rpcRequest()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'RegionId' => 'cn-hangzhou',
                        'PhoneNumbers' => $phoneNumber,//目标手机号
                        'SignName' => $signName,
                        'TemplateCode' => $templateCode,
                        'TemplateParam' => $jsonTemplateParam,
                    ],
                ])
                ->request();
            $opRes = $result->toArray();
            // print_r($opRes);
//            Array
//            (
//                [Message] => OK
//                [RequestId] => BE67A8-XXX-XXX-XXX-942EB9
//                [BizId] => 284407XXXXXXXXX55^0
//                [Code] => OK
//            )
            if ($opRes && $opRes['Code'] == "OK"){
                return true;
            }
        } catch (ClientException $e) {
            // echo $e->getErrorMessage() . PHP_EOL;
            return false;
        } catch (ServerException $e) {
            // echo $e->getErrorMessage() . PHP_EOL;
            return false;
        }
        return false;
    }
}

 



分享
0 条讨论
top