zoukankan      html  css  js  c++  java
  • 阿里云 短信消息api 示例 (附:阿里云控制台的消息服务,集成到codeigniter )

        <?php
        require_once(dirname(dirname(dirname(__FILE__))).'/act/alisms/mns-autoloader.php');
        use AliyunMNSClient;
        use AliyunMNSTopic;
        use AliyunMNSConstants;
        use AliyunMNSModelMailAttributes;
        use AliyunMNSModelSmsAttributes;
        use AliyunMNSModelBatchSmsAttributes;
        use AliyunMNSModelMessageAttributes;
        use AliyunMNSExceptionMnsException;
        use AliyunMNSRequestsPublishMessageRequest;
        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 "
    ";
                    echo $res->getMessageId();
                    echo "
    ";
                }
                catch (MnsException $e)
                {
                    echo $e;
                    echo "
    ";
                }
            }
        }
        $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 AliyunMNSClient;  
    4. use AliyunMNSTopic;  
    5. use AliyunMNSConstants;  
    6. use AliyunMNSModelMailAttributes;  
    7. use AliyunMNSModelSmsAttributes;  
    8. use AliyunMNSModelBatchSmsAttributes;  
    9. use AliyunMNSModelMessageAttributes;  
    10. use AliyunMNSExceptionMnsException;  
    11. use AliyunMNSRequestsPublishMessageRequest;  
    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 " ";  
    52. //            echo $res->getMessageId();  
    53. //            echo " ";  
    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
  • 相关阅读:
    sstream && istringstream && ostringstream
    动态规划
    状态空间搜索(转)
    数学知多少
    挣得值分析法
    adobe acrobat pro 8.0/8.1 激活方法
    ZooKeeper Overview
    Java网络编程从入门到精通(25):创建ServerSocket对象
    使用 Spring 2.5 基于注解驱动的 Spring MVC(二)
    一起偶遇网随机视频测试版
  • 原文地址:https://www.cnblogs.com/ericyuan/p/7260834.html
Copyright © 2011-2022 走看看