群控系统服务端开发模式-应用开发-短信工厂腾讯云短信开发

一、腾讯云短信工厂开发

1、添加框架对应的SDK

复制代码
composer require tencentcloud/tencentcloud-sdk-php

2、添加腾讯云工厂

在根目录下extend文件夹下Sms文件夹下channel文件夹下,创建腾讯云短信发送工厂并命名为TencentSmsSender。记住,一定要在腾讯云短信发送工厂类名后面去实现短信发送工厂。

复制代码
<?php
/**
 * 腾讯云短信发送
 * User: 龙哥 三年风水
 * Date: 2024/12/1/0001
 * Time: 16:51
 */
namespace Sms\channel;
use Error\BaseError;
use Sms\SmsSenderInterface;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Sms\V20190711\SmsClient;

class TencentSmsSender implements SmsSenderInterface
{
    protected static $signName = ""; //签名
    protected static $appId = ""; //APPid
    protected static $smsClient = null; //客户端

    public function __construct($param){
        self::$signName = $param['sign_name'];
        self::$appId = $param['app_id'];
        $cred = new Credential($param['access_key_id'],$param['access_key_secret']);
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("sms.tencentcloudapi.com");
        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        self::$smsClient = new SmsClient($cred, "ap-nanjing", $clientProfile);
    }

    /**
     * 单条短信发送
     * 内部json处理过
     * User: 龙哥·三年风水
     * Date: 2024/12/1/0001
     * Time: 15:52
     * @ param $mobile 要发送的对象
     * @ param $templateCode 模板编号
     * @ param $templateParam 模板对应参数
     * @ param string $signName 签名(如果需要采用不同的签名才需要传参)
     * @ return mixed
     */
    public static function send($mobile, $templateCode, $templateParam, $signName = '')
    {
        try {
            if(empty($mobile))throw new BaseError('手机号参数必须传',50000,200);
            if(is_array($mobile) && count($mobile) >= 0)throw new BaseError('手机号参数只能是字符串方式',50000,200);
            $req = new SendSmsRequest();
            $req->SmsSdkAppid = self::$appId;
            $req->Sign = empty($signName) ? self::$signName : $signName;
            $req->TemplateID = $templateCode;
            $req->TemplateParamSet = $templateParam;
            $req->PhoneNumberSet = array("+86".$mobile);
            $res = self::$smsClient->SendSms($req);
            if($res->SendStatusSet[0]->Code == "Ok")return true;
        }catch(TencentCloudSDKException $e) {
            throw new BaseError('发送失败',50000,200);
        }
    }

    /**
     * 批量短信发送
     * 内部json处理过,单个模板,单个签名
     * User: 龙哥·三年风水
     * Date: 2024/12/2
     * Time: 13:42
     * @ param $mobiles 要发送的对象,数组形式
     * @ param $templateCode 要发送的模板
     * @ param $templateParam 要发送的参数
     * @ param string $signName 签名(如果需要采用不同的签名才需要传参)
     * @ return mixed
     */
    public static function batchSend($mobiles, $templateCode, $templateParam, $signName = '')
    {
        try {
            if(!is_array($mobiles) || count($mobiles) == 0)throw new BaseError("批量发送,手机号码必须是数组",50000,200);
            $phoneNumbers = [];
            for($i = 0; $i < count($mobiles); $i++) {
                array_push($phoneNumbers, '+86'.$mobiles[$i]);
            }
            $req = new SendSmsRequest();
            $req->SmsSdkAppid = self::$appId;
            $req->Sign = empty($signName) ? self::$signName : $signName;
            $req->TemplateID = $templateCode;
            $req->TemplateParamSet = $templateParam;
            $req->PhoneNumberSet = $phoneNumbers;
            $res = self::$smsClient->SendSms($req);
            if($res->SendStatusSet[0]->Code == "Ok")return true;
        }catch(TencentCloudSDKException $e) {
            throw new BaseError('发送失败',50000,200);
        }
    }
}

二、测试

1、单条短信发送测试

复制代码
<?php
namespace app\controller;
use Encipher\Encrypt;
use Sms\SmsSenderFactory;

class Index extends Emptys
{
    public function index()
    {
        $smsSender = SmsSenderFactory::create();
        $code = random_int(100000, 999999);
        $smsSender::send('15088888888','123456789',[$code.'']);
        return succ('操作成功');
    }
}

2、批量短信发送测试

复制代码
<?php
namespace app\controller;
use Encipher\Encrypt;
use Sms\SmsSenderFactory;

class Index extends Emptys
{
    public function index()
    {
        $smsSender = SmsSenderFactory::create();
        $code = random_int(100000, 999999);
        $smsSender::batchSend(['15088888888','15188888888'],'123456789',[$code.'']);
        return succ('操作成功');
    }
}
相关推荐
BingoGo5 小时前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack5 小时前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack1 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo1 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
初次攀爬者2 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
JaguarJack2 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
QQ5110082853 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe3 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5
longxiangam3 天前
Composer 私有仓库搭建
php·composer