阿里云短信接入 PHP

php 复制代码
<?php

namespace app\common\controller;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use think\facade\Config as tConfig;
 
class AliSms  {
 
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret,$endpoint){
        $config = new Config([
            // 您的 AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的 AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = $endpoint;
        return new Dysmsapi($config);
    }
 
    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        $client = self::createClient($args['access_key_id'], $args['access_key_secret'],$args['endpoint']);
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => $args['phone'],  //发送的手机号码
            "signName" => $args['sign_name'],  //签名
            "templateCode" => $args['template'],  //模板CODE
            "templateParam" => json_encode($args['info'])  //这个根据你的模板信息来
        ]);
 
        $result = $client->sendSms($sendSmsRequest);
        if ($result->body->message == 'OK' && $result->body->code == 'OK') {
            return ['code' => 200,'msg'=>'success'];
        }
        if ($result->body->code == 'isv.MOBILE_NUMBER_ILLEGAL') {
            return ['code' => 0, 'msg' => '手机号码格式不正确'];
        }
        return ['code' => 0, 'msg' => '短信发送失败,网络繁忙'];
    }
}
相关推荐
twcc_come16 小时前
安全策略配置实验报告
开发语言·php
weixin_BYSJ198717 小时前
springboot3家政平台小程序--附源码00904
java·javascript·spring boot·python·django·flask·php
上海安当技术20 小时前
车间电脑指纹登录怎么做?从 FIDO2 公钥体系到统一身份认证的技术拆解
开发语言·电脑·php
大黄说说2 天前
接口频繁超时、报错?后端常见排查思路总结
开发语言·php
Sagittarius_A*2 天前
【RCELABS】Level 1~4 —— 基础篇
靶场·php·ctf·代码审计·rce
老猿AI洞察2 天前
阿里Qwen-Image-3.0发布:4.5K token长输入+10px小字渲染,AI生图终于能从“好看“走向“好用“了
人工智能·阿里云
海天鹰2 天前
PHP换行
php
moonsims2 天前
星闪在跨域无人化系统作用
开发语言·php
BullSmall3 天前
CentOS7 阿里云 PG 镜像 PG10.9 → PG17 完整升级指南
阿里云·postgresql·centos
TMT星球3 天前
IDC报告:阿里云稳居中国混合云软件及服务市场第一
阿里云·云计算