阿里云短信接入 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' => '短信发送失败,网络繁忙'];
    }
}
相关推荐
匀泪6 分钟前
CE(NFS服务器)
运维·服务器·php
橘子137 分钟前
MySQL基础(一)
数据库·mysql·php
mixboot1 小时前
aliyun阿里云Instant计算服务IP地址查询脚本
阿里云·instant
@YDWLCloud2 小时前
华为云国际版 vs 阿里云国际版:东南亚市场选型指南
大数据·服务器·阿里云·华为云·云计算
Hi202402173 小时前
使用Smokeping监控公网服务器质量
运维·服务器·php·smokeping
酒书3 小时前
springcloud阿里云OCR(个人证照)识别对接
阿里云·云计算·ocr
翼龙云_cloud3 小时前
阿里云渠道商:阿里云弹性伸缩混合管理指南
服务器·阿里云·云计算
码农水水4 小时前
京东Java面试被问:Spring Boot嵌入式容器的启动和端口绑定原理
java·开发语言·人工智能·spring boot·面试·职场和发展·php
运筹vivo@4 小时前
攻防世界: simple_php
android·php·android studio
21my_code4 小时前
阿里云资源
阿里云·云计算