阿里云短信接入 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' => '短信发送失败,网络繁忙'];
    }
}
相关推荐
同聘云14 小时前
阿里云云服务器云备份满了可以删除吗?不小心把备份删除了怎么办
服务器·阿里云·云计算
TG:@yunlaoda360 云老大16 小时前
如何在华为云国际站代理商控制台进行SFS Turbo的基础状态核查?
大数据·服务器·华为云·php
数据库知识分享者小北16 小时前
Hybrid Model Support:阿里云 Tair 联合 SGLang对 Mamba-Transformer 等混合架构模型的支持方案
数据库·阿里云·tair
花哥码天下16 小时前
阿里云效流水线部署springboot项目-2026
阿里云·云计算
韩立学长18 小时前
【开题答辩实录分享】以《学生心理预防监控信息系统的设计与实现开题报告》为例进行选题答辩实录分享
mysql·php
zzoood19 小时前
【PHP】富文本编辑器图片自动追加域名
开发语言·php
catchadmin19 小时前
PHP 初学者指南 基础结构与常见错误
php
柠檬汁Dev19 小时前
训练模型用GCP,推理服务放阿里云?聊聊AIGC时代的多云自由
阿里云·云计算·aigc
翼龙云_cloud19 小时前
阿里云渠道商:如何快速解决更换阿里云GPU公网IP后出现的网络故障?
运维·tcp/ip·阿里云·云计算