阿里云短信接入 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' => '短信发送失败,网络繁忙'];
    }
}
相关推荐
用户92724725021918 小时前
PHP+JS+CSS+JSON 单页新闻系统实现方案
php
Ashlee_code21 小时前
什么是Web3?金融解决方案
开发语言·金融·架构·eclipse·web3·区块链·php
Sally璐璐1 天前
IPSAN 共享存储详解:架构、优化与落地实践指南
开发语言·php
程序猿阿伟1 天前
《声音的变形记:Web Audio API的实时特效法则》
开发语言·前端·php
Clownseven1 天前
Shell 脚本实战指南:内网 ARP 洪泛监控与飞书/邮件自动告警
网络·php·飞书
浪裡遊2 天前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
ejinxian2 天前
PHP 超文本预处理器 发布 8.5 版本
开发语言·php
zorro_z2 天前
PHP语法基础篇(九):正则表达式
php
Imagine Miracle2 天前
Ubuntu for ARM 更换为阿里云镜像源
arm开发·ubuntu·阿里云
高压锅_12202 天前
思科与华为网络设备命令对比指南:从基础操作到高级配置
服务器·华为·php