ericyuan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
    <?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个参数(手机号,模版,模版里的参数数组)

  1. <?php  
  2. require_once APP_FOLDER.'/third_party/aliyunsms/mns-autoloader.php';  
  3. use AliyunMNS\Client;  
  4. use AliyunMNS\Topic;  
  5. use AliyunMNS\Constants;  
  6. use AliyunMNS\Model\MailAttributes;  
  7. use AliyunMNS\Model\SmsAttributes;  
  8. use AliyunMNS\Model\BatchSmsAttributes;  
  9. use AliyunMNS\Model\MessageAttributes;  
  10. use AliyunMNS\Exception\MnsException;  
  11. use AliyunMNS\Requests\PublishMessageRequest;  
  12.   
  13. class Sms {  
  14.     public function run($mobile, $template_code, $pars = array()) {  
  15.         $CI  =& get_instance();  
  16.         /** 
  17.          * Step 1. 初始化Client 
  18.          */  
  19.         $this->endPoint = $CI->config->item('aliyun_sms')['endPoint'];  
  20.         $this->accessId = $CI->config->item('aliyun_sms')['accessId'];  
  21.         $this->accessKey = $CI->config->item('aliyun_sms')['accessKey'];  
  22.         $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);  
  23.         /** 
  24.          * Step 2. 获取主题引用 
  25.          */  
  26.         $topicName = $CI->config->item('aliyun_sms')['topicName'];//"sms.topic-cn-hangzhou";  
  27.         $topic = $this->client->getTopicRef($topicName);  
  28.         /** 
  29.          * Step 3. 生成SMS消息属性 
  30.          */  
  31.         // 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode)  
  32.         $batchSmsAttributes = new BatchSmsAttributes($CI->config->item('aliyun_sms')['SMSSignName'], $template_code);  
  33.         // 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值  
  34.         $batchSmsAttributes->addReceiver($mobile, $pars);  
  35.         // $batchSmsAttributes->addReceiver("YourReceiverPhoneNumber2", array("YourSMSTemplateParamKey1" => "value1"));  
  36.         $messageAttributes = new MessageAttributes(array($batchSmsAttributes));  
  37.         /** 
  38.          * Step 4. 设置SMS消息体(必须) 
  39.          * 
  40.          * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。 
  41.          */  
  42.         $messageBody = "smsmessage";  
  43.         /** 
  44.          * Step 5. 发布SMS消息 
  45.          */  
  46.         $request = new PublishMessageRequest($messageBody, $messageAttributes);  
  47.         try {  
  48.             $res = $topic->publishMessage($request);  
  49.             return $res->isSucceed();  
  50. //            echo $res->isSucceed();  
  51. //            echo "\n";  
  52. //            echo $res->getMessageId();  
  53. //            echo "\n";  
  54.         } catch (MnsException $e) {  
  55.             //记录错误日志,比如我的helper里面有个write_log方法  
  56. //            write_log($e);  
  57.             return false;  
  58.         }  
  59.     }  
  60. }  


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

    1. public function test() {  
    2.         $this->load->library('Sms');  
    3.   
    4.         $par = array(  
    5.             'number' => '123456'  
    6.         );  
    7.         $res = $this->sms->run('13512345678', $this->config->item('aliyun_sms')['templateCode']['vlidate_code'], $par);  
    8.         echo $res;  
    9.     } 
参考文档:http://blog.csdn.net/michaelzhouh/article/details/72830509
posted on 2017-07-30 22:40  ericyuan  阅读(991)  评论(0编辑  收藏  举报