阿里云 短信消息api 示例 (附:阿里云控制台的消息服务,集成到codeigniter )
Posted ericyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云 短信消息api 示例 (附:阿里云控制台的消息服务,集成到codeigniter )相关的知识,希望对你有一定的参考价值。
<?php require_once(dirname(dirname(dirname(__FILE__))).‘/act/alisms/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 PublishBatchSMSMessageDemo { public function run() { /** * Step 1. 初始化Client */ $this->endPoint = "http://abc.mns.cn-hangzhou.aliyuncs.com/"; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com $this->accessId = "abc"; $this->accessKey = "abc"; $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey); /** * Step 2. 获取主题引用 */ $topicName = "sms.topic-cn-hangzhou"; $topic = $this->client->getTopicRef($topicName); /** * Step 3. 生成SMS消息属性 */ // 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode) $batchSmsAttributes = new BatchSmsAttributes("abc", "SMS_80171111"); // 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值 $batchSmsAttributes->addReceiver("13505111111", array("code" => "3120")); //$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); echo $res->isSucceed(); echo "\n"; echo $res->getMessageId(); echo "\n"; } catch (MnsException $e) { echo $e; echo "\n"; } } } $instance = new PublishBatchSMSMessageDemo(); $instance->run(); ?>
首先声明不是阿里大鱼短信平台(其实阿里大鱼短信的集成反而简单些)
官方的文档是: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;
- }
参考文档:http://blog.csdn.net/michaelzhouh/article/details/72830509
以上是关于阿里云 短信消息api 示例 (附:阿里云控制台的消息服务,集成到codeigniter )的主要内容,如果未能解决你的问题,请参考以下文章