thinkphp6使用阿里云SDK发送短信

使用composer安装sdk

php 复制代码
   "alibabacloud/dysmsapi-20170525": "2.0.24"

封装发送短信类

发送到的短信参数写在env文件里面的

php 复制代码
#发送短信配置
[AliyunSms]
AccessKeyId = ""
AccessKeySecret =""
signName=""
templateCode=""
php 复制代码
<?php


namespace app\api\service;







use AlibabaCloud\Credentials\Credential\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Exception;
use think\facade\Env;

class AliSms
{


    /**
     * 发送短信验证码
     *
     * @param string $phoneNumber 手机号码
     * @param string $code 验证码
     * @return bool 发送成功返回true,否则返回false
     */
    public static function  sendSmsCode($phoneNumber, $code):array
    {
        // 阿里云控制台中获取的AccessKey信息
        $accessKeyId = Env::get("AliyunSms.AccessKeyId");
        $accessSecret = Env::get("AliyunSms.AccessKeySecret");

// 短信模板CODE,根据自己的模板填写
        $templateCode = Env::get("AliyunSms.templateCode");

// 短信签名,根据自己的签名填写
        $signName = Env::get("AliyunSms.signName");
        try {
            $config = new Config([
                // 必填,您的 AccessKey ID
                "accessKeyId" => $accessKeyId,
                // 必填,您的 AccessKey Secret
                "accessKeySecret" => $accessSecret
            ]);
            // 访问的域名
            $config->endpoint = "dysmsapi.aliyuncs.com";
            $client = new Dysmsapi($config);

            $sendSmsRequest = new SendSmsRequest([
                "phoneNumbers" => $phoneNumber,
                "signName" => $signName,
                "templateCode" => $templateCode,
                "templateParam" => json_encode(array('code' => $code))
            ]);
            $runtime = new RuntimeOptions([]);


            // 复制代码运行请自行打印 API 的返回值
            $result = $client->sendSmsWithOptions($sendSmsRequest, $runtime);


            if ($result->statusCode == 200) {
                return ["code" => 1, "msg" => "短信发送成功"];
            } else {
                // 短信发送失败
                return ["code" => -1, "msg" => "发送失败"];
            }
        } catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 如有需要,请打印 error

            return ["code" => -1, "msg" =>    Utils::assertAsString($error->message)];

        }


    }

}

调用方法

php 复制代码
  $code = rand(1000, 9999);
  $res = AliSms::sendSmsCode($phone, $code);
相关推荐
CRMEB定制开发9 小时前
CRMEB 框架中 PHP 上传扩展开发:涵盖本地上传及阿里云 OSS、腾讯云 COS、七牛云
阿里云·php·腾讯云·微信商城·php商城源码
小鱼小鱼.oO21 小时前
阿里云服务器安装nginx并配置前端资源路径(前后端部署到一台服务器并成功访问)
服务器·nginx·阿里云
avoidaily1 天前
使用Node.js分片上传大文件到阿里云OSS
阿里云·node.js·云计算
国际云2 天前
腾讯云国际版和国内版账户通用吗?一样吗?为什么?
大数据·运维·阿里云·云计算
BOB-wangbaohai2 天前
阿里云ACP云计算备考笔记 (3)——云存储&RDS
阿里云·云计算·oss·块存储·rds
BOB-wangbaohai3 天前
阿里云ACP云计算备考笔记 (3)——云服务器ECS
阿里云·云计算·安全组·ecs·云盘
壬戌知秋3 天前
yum更换阿里云的镜像源
阿里云·云计算
国际云,接待3 天前
阿里云无影云桌面深度测评
运维·服务器·阿里云·云原生·云计算
arui23 天前
Docker部署与应用、指令
阿里云·docker·容器
国际云,接待4 天前
阿里云国际站,如何通过代理商邀请的链接注册账号
运维·服务器·科技·阿里云·云计算