当前位置: 首页 > article >正文

ThinkPHP腾讯云国际短信对接

composer require tencentcloud/tencentcloud-sdk-php安装composer包

    public function tencent_send(){
        $mobile = $this->request->post("mobile");
//        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
//            $this->error(__('手机号不正确'));
//        }
        $sms = new SmsTencent();
        $auth_code = rand(1000,9999);
        $result = $sms->send(Config::get('site.tencent_template_id'), '', $mobile, $auth_code);
        if ($result['code'] == 0) {
            Cache::set($mobile,$auth_code,300);
            $this->success(__('发送成功'));
        } else {
            $this->error('发送失败:'.$result['msg']);
        }
    }




<?php

namespace app\api\controller;

use app\common\controller\Api;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Sms\V20190711\SmsClient;
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use think\Config;

class SmsTencent extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        // 这里的配置信息可以写入基本配置里面,这里只是为了练手
        $this->config = [
            'appid' => Config::get('site.tencent_appid'),
            'secret_id' => Config::get('site.tencent_secret_id'),
            'secret_key' => Config::get('site.tencent_secret_key')
        ];
    }

    public function send($template_id, $sign, $phone, $auth_code)
    {

        $return = [
            'code' => 0,
            'msg' => 'success',
            'data' => []
        ];

        try {

            $cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);

            $req = new SendSmsRequest();
            /* 是否国际/港澳台短信:
               0表示国内短信
               1表示国际/港澳台短信 */
            $req->International = 1;

            $params = array(
                "PhoneNumberSet" => array(
                    $phone
                ),
                "TemplateID" => $template_id,
                "Sign" => $sign,
                "TemplateParamSet" => array(
                    (string)$auth_code
                ),
                "SmsSdkAppid" => $this->config['appid']
            );
            $req->fromJsonString(json_encode($params));

            $resp = $client->SendSms($req);

            $result = $resp->toJsonString();
            $result = json_decode($result, true);

        } catch (TencentCloudSDKException $e) {
            $return['code'] = 500;
            $return['msg'] = $e->getMessage();
            return $return;
        }

        if (isset($result['SendStatusSet'][0]['Code'])&&$result['SendStatusSet'][0]['Code']=='Ok') {
            $return['code'] = 0;
            $return['msg'] = 'success';
        } else {
            $return['code'] = 500;
            $return['msg'] = $result['SendStatusSet'][0]['Message'] ?? '发送失败';
        }

        return $return;
    }
}


http://www.kler.cn/a/382333.html

相关文章:

  • 探索PickleDB:Python中的轻量级数据存储利器
  • Python的条件语句if与match...case
  • 揭秘自闭症症状的最新研究成果和应对策略
  • SAP RFC 用户安全授权
  • leetcode | 88. 合并两个有序数组
  • JavaScript数据类型- Symbol 详解
  • W5100S-EVB-Pico2评估板介绍
  • 史上最全盘点:一文告诉你低代码(Low-Code)是什么?为什么要用?
  • 【青牛科技】GC8549替代LV8549/ONSEMI在摇头机、舞台灯、打印机和白色家电等产品上的应用分析
  • 100种算法【Python版】第48篇——计数排序
  • CNN在线识别手写中文
  • 小区搜索和SSB简介
  • Rust 异步编程实战
  • 总结:Vue2中双向绑定不生效的排查方法及原理
  • [云讷科技]DASA数字孪生机器人概念
  • 【5.8】指针算法-双指针验证回文串
  • 小语言模型介绍与LLM的比较
  • 【d63】【Java】【力扣】141.训练计划III
  • MFC,DLL界面库设计注意
  • 基于uniapp和java的电动车智能充电系统软件平台的设计
  • html checkbox和label 文字不对齐解决办法
  • 某华迪加现场大屏互动系统mobile.do.php任意文件上传
  • windows安装mysql
  • Android 依赖统一配置管理(Version Catalogs)
  • 学习党的二十大精神,推动科技创新和发展
  • Spring中的资源Resource 以及分类(多种资源的实现类)