ali-mns icon indicating copy to clipboard operation
ali-mns copied to clipboard

可以给个发送手机短信的基本案例吗

Open nihaoit opened this issue 7 years ago • 0 comments

非常感谢大神提供SDK,是否可以提供个发送手机短信的案例代码呢? 这是官方提供的PHP的发送短信的代码案例

 public function run()
    {
        /**
         * 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";
        $topic = $this->client->getTopicRef($topicName);
        /**
         * Step 3. 生成SMS消息属性
         */
        // 3.1 设置发送短信的签名(SMSSignName)和模板(SMSTemplateCode)
        $batchSmsAttributes = new BatchSmsAttributes("YourSMSSignName", "YourSMSTemplateCode");
        // 3.2 (如果在短信模板中定义了参数)指定短信模板中对应参数的值
        $batchSmsAttributes->addReceiver("YourReceiverPhoneNumber1", array("YourSMSTemplateParamKey1" => "value1"));
        $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();

我因为是PHP转nodejs的新手,对着这个代码来看ali-mns,readme中的代码,感觉差别好大,读了很久读不懂。期待回复^_^

nihaoit avatar Jun 19 '17 03:06 nihaoit