阿里云控制台的消息服务,集成到codeigniter

本文介绍如何在项目中集成阿里云短信服务,包括SDK下载、配置文件添加及短信发送实现等步骤。通过简单的示例代码展示如何快速实现短信验证码等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先声明不是阿里大鱼短信平台(其实阿里大鱼短信的集成反而简单些)

官方的文档是:https://help.aliyun.com/document_detail/51929.html

1. SDK下载和引入

我下载的版本是Version1.3.4,更新日期是2017-4-13,估计大家用的时候,可能都有新版本了。放在third_party下:


2. 添加短信配置文件


3. libraries添加Sms.php(名字可以自己定义)

其实也就是根据官方那个demo,添加了调用ci的短信配置文件,run方法只需要3个参数(手机号,模版,模版里的参数数组)

<?php
require_once APP_FOLDER.'/third_party/aliyunsms/mns-autoloader.php';
use AliyunMNS\Client;
use AliyunMNS\Topic;
use AliyunMNS\Constants;
use AliyunMNS\Model\MailAttributes;
use AliyunMNS\Model\SmsAttributes;
use AliyunMNS\Model\BatchSmsAttributes;
use AliyunMNS\Model\MessageAttributes;
use AliyunMNS\Exception\MnsException;
use AliyunMNS\Requests\PublishMessageRequest;

class Sms {
    public function run($mobile, $template_code, $pars = array()) {
        $CI  =& get_instance();
        /**
         * Step 1. 初始化Client
         */
        $this->endPoint = $CI->config->item('aliyun_sms')['endPoint'];
        $this->accessId = $CI->config->item('aliyun_sms')['accessId'];
        $this->accessKey = $CI->config->item('aliyun_sms')['accessKey'];
        $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
        /**
         * Step 2. 获取主题引用
         */
        $topicName = $CI->config->item('aliyun_sms')['topicName'];//"sms.topic-cn-hangzhou";
        $topic = $this->client->getTopicRef($topicName);
        /**
         * Step 3. 生成SMS消息属性
         */
        // 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode)
        $batchSmsAttributes = new BatchSmsAttributes($CI->config->item('aliyun_sms')['SMSSignName'], $template_code);
        // 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值
        $batchSmsAttributes->addReceiver($mobile, $pars);
        // $batchSmsAttributes->addReceiver("YourReceiverPhoneNumber2", array("YourSMSTemplateParamKey1" => "value1"));
        $messageAttributes = new MessageAttributes(array($batchSmsAttributes));
        /**
         * Step 4. 设置SMS消息体(必须)
         *
         * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
         */
        $messageBody = "smsmessage";
        /**
         * Step 5. 发布SMS消息
         */
        $request = new PublishMessageRequest($messageBody, $messageAttributes);
        try {
            $res = $topic->publishMessage($request);
            return $res->isSucceed();
//            echo $res->isSucceed();
//            echo "\n";
//            echo $res->getMessageId();
//            echo "\n";
        } catch (MnsException $e) {
            //记录错误日志,比如我的helper里面有个write_log方法
//            write_log($e);
            return false;
        }
    }
}

4. 大功告成,开始使用吧,随便在一个controller里面写个方法测试吧。比如测试一个短信验证码,只需要传一个number参数的

public function test() {
        $this->load->library('Sms');

        $par = array(
            'number' => '123456'
        );
        $res = $this->sms->run('13512345678', $this->config->item('aliyun_sms')['templateCode']['vlidate_code'], $par);
        echo $res;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值