tpshop 阿里雲簡訊sdk使用和帶連結簡訊實現
阿新 • • 發佈:2019-01-24
<?php
namespace Common\Util;
require_once('/ThinkPHP/Library/Vendor/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($mobilephone,$sendurl)
{
/**
* 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($mobilephone, array("YourSMSTemplateParamKey1" => $sendurl));
// $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";
}
}
}
?>