• 阿里云发送短信验证码


    废话不多说,直接干货:

    第一步:下载php sdk 地址:https://help.aliyun.com/document_detail/32381.html?spm=5176.doc51929.6.685.18WOhG

    结构如图:

    也不知道能用多少;反正都留着。

    第二步:打开https://help.aliyun.com/document_detail/51929.html?spm=5176.doc32381.6.688.sVNnNG

    仔细阅读本页面,设计具体的参数获取方法和位置。

    最后有实例代码,一个发送类: 重名为:sentmsg.php

    <?php
    require_once(dirname(dirname(dirname(__FILE__))).'/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($mobile,$code)
        {
            /**
             * Step 1. 初始化Client
             */
            $this->endPoint = "YourMNSEndpoint"; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com  按着样式写 可以找到的
            $this->accessId = "YourAccessId"; //重要参数
            $this->accessKey = "YourAccessKey";//重要参数
            $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
            /**
             * Step 2. 获取主题引用
             */
            $topicName = "YourTopicName";//主题名 类似 sms.topic-cn-xxxx
            $topic = $this->client->getTopicRef($topicName);
            /**
             * Step 3. 生成SMS消息属性
             */
            // 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode)
            $batchSmsAttributes = new BatchSmsAttributes("YourSMSSignName", "YourSMSTemplateCode");//YourSMSSignName 多为汉字。YourSMSTemplateCode 格式:SMS_xxxxxxxxxx 
            // 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值
         $batchSmsAttributes->addReceiver($mobile, array("code" => $code));
            $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();
    ?>
    

     以上代码中注释很必要。

    第三步:写一个php文件把上面的类引入

    require_once 'sentmsg.php';
    $mobile = (string)$_POST['mobile'];
    $code = (string)rand(100000,999999);
    $time = date("Y-m-d H:i:s");
    $instance = new PublishBatchSMSMessageDemo();
    $res = $instance->run($mobile,$code);
    if ($res = true) {
    	echo $code.','.$time;
    }	
    

     手机号和验证码必须是字符串!!! 手机号和验证码必须是字符串!!! 手机号和验证码必须是字符串!!!

    没了,是不是比把大象装冰箱还简单。

  • 相关阅读:
    168. Excel Sheet Column Title
    461. Hamming Distance
    Tree Representation Implementation & Traversal
    404. Sum of Left Leaves
    572. Subtree of Another Tree
    20. Valid Parentheses
    Check time of different search methods
    Binary search tree or not
    Coin Change
    JS DOM:文档对象模型 --树模型 文档:标签文档,对象:文档中每个元素对象,模型:抽象化的东西
  • 原文地址:https://www.cnblogs.com/guoyachao/p/7130691.html
Copyright © 2020-2023  润新知