分类
后端

阿里云短信

<?php
/**
 * @author: XiaKai
 * @email: 1030989241@qq.com
 * @since : 2020/5/21 15:00
 */

namespace app\common\controller;

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

class SmsController
{
    private $accessKeyId;
    private $accessSecret;
    private $SignName;
    private $TemplateCode;

    //测试发送短信入口
    public function index()
    {
        $phone = '15982484842';
        $code = rand('100000', '999999');
        $TemplateCode = 'TemplateCode1';
        $SmsController = new SmsController($TemplateCode);
        $SmsController->send($phone, ['code' => $code]);
    }

    //定义短信配置
    public function __construct($TemplateCode)
    {
        $option_value = Db::name("option")->where([['id', '=', 1]])->value('option_value');
        $data = json_decode($option_value, true);
        if (!isset($data['accessKeyId']) || !$data['accessKeyId']) {
            $this->returnMsg('accessKeyId未配置');
        }
        if (!isset($data['accessSecret']) || !$data['accessSecret']) {
            $this->returnMsg('accessSecret未配置');
        }
        if (!isset($data['SignName']) || !$data['SignName']) {
            $this->returnMsg('签名未配置');
        }
        if (!isset($data[$TemplateCode]) || !$data[$TemplateCode]) {
            $this->returnMsg('模板ID不存在');
        }
        $this->accessKeyId = $data['accessKeyId'];
        $this->accessSecret = $data['accessSecret'];
        $this->SignName = $data['SignName'];
        $this->TemplateCode = $data[$TemplateCode];
    }

    //发送短信核心代码
    public function send($phone, $data)
    {
        //短信内容变量
        $data = json_encode($data);
        try {
            AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessSecret)
                ->regionId('cn-hangzhou')
                ->asDefaultClient();
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => $this->SignName,
                        'TemplateCode' => $this->TemplateCode,
                        'TemplateParam' => $data
                    ],
                ])
                ->request();
            $result_arr = $result->toArray();
            if ($result_arr['Code'] != 'OK') {
                $this->returnMsg('发送失败');
            }
        } catch (ClientException $e) {
            $this->returnMsg($e->getErrorMessage() . PHP_EOL);
        } catch (ServerException $e) {
            $this->returnMsg($e->getErrorMessage() . PHP_EOL);
        }
        $this->returnMsg('发送成功', 1);
    }

    private function returnMsg($msg = '', $code = 0, $data = [])
    {
        die(json_encode(['msg' => $msg, 'code' => $code, 'data' => $data]));
    }
}

DEMO下载 放在/app/ 目录下
https://www.blog.zuocaibang.cn/wp-content/uploads/2020/05/common.zip